Javascript 倒计时计时器仅在页面刷新时停止
我从stackoverflow借用了一个倒计时计时器代码,它工作得很好,直到我决定从stackoverflow添加另一个使用本地存储的代码,这样即使在页面刷新后计时器也会继续。我的计时器应该在totalSeconds为0时停止,但由于存在本地存储,它不知道totalSeconds的值应该是多少。totalSeconds的if语句正确吗?它只在页面刷新时工作,但在倒计时时无法识别计时器Javascript 倒计时计时器仅在页面刷新时停止,javascript,Javascript,我从stackoverflow借用了一个倒计时计时器代码,它工作得很好,直到我决定从stackoverflow添加另一个使用本地存储的代码,这样即使在页面刷新后计时器也会继续。我的计时器应该在totalSeconds为0时停止,但由于存在本地存储,它不知道totalSeconds的值应该是多少。totalSeconds的if语句正确吗?它只在页面刷新时工作,但在倒计时时无法识别计时器 quiztime: res.data[x].Time; //This was a variable from
quiztime: res.data[x].Time; //This was a variable from my get function that based on user input
var totalSeconds = 0;
if (this.quizTime != 0) {
if(localStorage.getItem("totalSeconds")){
totalSeconds = localStorage.getItem("totalSeconds");
} else {
totalSeconds = this.quizTime;
}
}
if(totalSeconds <= 0){
setTimeout(this.submitMyAnswer(), 3000);
} else {
setTimeout(setTime, 1000);
}
var minutesLabel = document.getElementById("minutes");
var secondsLabel = document.getElementById("seconds");
setInterval(setTime, 1000);
function setTime() {
--totalSeconds;
secondsLabel.innerHTML = pad(totalSeconds % 60);
minutesLabel.innerHTML = pad(parseInt(totalSeconds / 60));
localStorage.setItem("totalSeconds", totalSeconds);
}
function pad(val) {
var valString = val + "";
if (valString.length < 2) {
return "0" + valString;
} else {
return valString;
}
}
window.addEventListener("beforeunload", function (e) {
e.preventDefault();
s;
e.returnValue = "";
});
});
},
quiztime:res.data[x].时间//这是一个基于用户输入的get函数变量
var totalSeconds=0;
如果(this.quizTime!=0){
if(localStorage.getItem(“totalSeconds”)){
totalSeconds=localStorage.getItem(“totalSeconds”);
}否则{
totalSeconds=this.quizTime;
}
}
如果(totalSeconds您需要将setTimeout
的返回值存储在变量中。然后,您只需使用clearTimeout
方法清除间隔
例如:
const milisecs=1000//1秒
常量超时=设置超时(()=>{
//在这里做些事情
},毫秒)
//在做了其他事情之后…再见亲爱的暂停,我们不会忘记你的
clearTimeout(超时)
同样的事情也适用于setInterval
和clearInterval
代码中几乎没有错误
不要在settmout
回调中使用括号()
(setTimeout(this.submitMyAnswer(),3000);
)。因为它将在超时后调用submitMyAnswer()的返回值。我想你应该在3秒后调用submitMyAnswer
,所以只需使用
setTimeout(this.submitMyAnswer,3000);
另一件事是要在totalSeconds
变为0后清除间隔。若要停止间隔,还应使用参考
在您的情况下,您必须将setInterval
的返回值分配给任何对象,比如interval
,当setTime
中添加条件时,当totalSeconds声明totalSeconds
在if(this.quizTime!=0)之外
block好的,我会试试。谢谢你的回答,但是如果我的totalSeconds等于0,我想停止执行该怎么办?就像在计时器上一样,但不刷新?我需要编辑我的问题,因为我对我的问题有点困惑。因此,如果我清除了间隔,它会将totalSecond计算为我的quizTime吗?我想在quizTime过期后执行一个函数,但由于它位于localStorage中,因此它只在刷新时执行。您想在totalSeconds
变为0时执行submitMyAnswer
吗?是的,一旦quizTime或作为localStorage数据的totalSeconds过期,它应该执行该函数。quizTime就是main时间,而localStorage是剩余时间。很抱歉,如果混淆的话,我认为两者是相同的,因为它们都是在totalSeconds上声明的。我在submitMyAnswer函数上收到一个错误。它说它不是一个函数。这可能与函数的范围有关,请检查更新的答案。
// Comment below code. It will not needed.
//if (totalSeconds <= 0) {
// setTimeout(this.submitMyAnswer, 3000);
//} else {
// setTimeout(setTime, 1000);
//}
// assign return value from setInterval to object
// pass this.submitMyAnswer function as parameter so it can be invoked when totalSeconds become 0, Note : Do not use () as this.submitMyAnswer()
var interval = setInterval(setTime, 1000, this.submitMyAnswer);
// get callback function submitMyAnswer in parameter to invoke when time elapsed.
function setTime(submitMyAnswer) {
--totalSeconds;
secondsLabel.innerHTML = pad(totalSeconds % 60);
minutesLabel.innerHTML = pad(parseInt(totalSeconds / 60));
localStorage.setItem("totalSeconds", totalSeconds);
// clear interval once totalSeconds is 0
if (totalSeconds <= 0) {
clearInterval(interval);
// call submitMyAnswer() once totalSeconds become 0.
// do not use this.submitMyAnswer(), simply use submitMyAnswer()
submitMyAnswer();
}
}