如何将时间保存在计时器中,然后再继续



所以我有一个计时器和一个使用JSON的保存文件。json文件就是这样的,例如:

{
"name": "shinx",
"imageurl": "https://poketch-cdn-assets.s3.amazonaws.com/images/pokemon/animated/shiny/shinx.gif",
"method": "pokeradar",
"counter": 0,
"shinycharm": false,
"masuda": false
}

这是计时器代码:

// Convert time to a format of hours, minutes, seconds, and milliseconds
function timeToString(time) {
let diffInHrs = time / 3600000;
let hh = Math.floor(diffInHrs);
let diffInMin = (diffInHrs - hh) * 60;
let mm = Math.floor(diffInMin);
let diffInSec = (diffInMin - mm) * 60;
let ss = Math.floor(diffInSec);
let diffInMs = (diffInSec - ss) * 100;
let ms = Math.floor(diffInMs);
let formattedHH = hh.toString().padStart(2, "0");
let formattedMM = mm.toString().padStart(2, "0");
let formattedSS = ss.toString().padStart(2, "0");
let formattedMS = ms.toString().padStart(2, "0");
return `${formattedHH}:${formattedMM}:${formattedSS}`;
}
// Declare variables to use in our functions below
let startTime;
let elapsedTime = 0;
let timerInterval;
// Create function to modify innerHTML
function print(txt) {
document.getElementById("Timer").innerHTML = txt;
}
// Create "start", "pause" and "reset" functions
function start() {
startTime = Date.now() - elapsedTime;
timerInterval = setInterval(function printTime() {
elapsedTime = Date.now() - startTime;
print(timeToString(elapsedTime));
}, 10)
}
function pause() {
clearInterval(timerInterval);
}
// Create event listeners
let playButton = document.getElementById("playButton");
let pauseButton = document.getElementById("pauseButton");
playButton.addEventListener("click", start);
pauseButton.addEventListener("click", pause);

我保存json文件的方法如下:

document.getElementById("saveButton").addEventListener('click',()=>{
//Save the counter here (there is a code above that counts click ignore this one, not related to the question)
finalcurrenthunt.counter=counter;
//save the timer here
//timer save code (what variables do I need to save inside the json file in order to continue from where I paused the timer)
//save the json to a selected folder 
const huntsaved = JSON.stringify(finalcurrenthunt,null,2);
const finalhuntsaved = huntsaved.replaceAll('n',"")
alert("Please Click On The Save File, When Asked To Replace Please Do So.")
ipcRenderer.send("create-document-trigger",finalhuntsaved);
})

我想在暂停计时器后保存它,当我退出应用程序并再次启动它时,加载保存文件,我将从上次保存的值开始。

示例:我一直使用计时器直到到达00:0025,当计时器在00:00:25时,我保存了文件,我想再次使用计时器,所以我打开应用程序并加载保存文件以从00:00:25继续,而不是完全重新启动计时器

我怎样才能做到这一点?

我的问题很直接,我不得不用一种奇怪的方式来节省时间?

我把let ElapsedTime推到了文件的顶部(因为你在这里看到的一切都在我的js底部(在清除Interval 之前,我启用了暂停功能

saveTimer = ElapsedTime;

然后转到我的保存方法并添加

finalcurrenthunt.timer=saveTimer;

相关内容

  • 没有找到相关文章