[英]setInterval keeps running after clearTimeout has been called
I declared a global variable sim in which I initialized setInterval. 我声明了一个全局变量sim,在其中初始化了setInterval。 In progressSim function I used clearTimeout() but it does not stop the setInterval. 在progressSim函数中,我使用了clearTimeout(),但它不会停止setInterval。
var sim;
function loop(){
var ctx1 = document.getElementById($(allChild[i]).attr('id')).getContext('2d');
dataPercent = $(allChild[i]).attr('data-percent');
cw = ctx1.canvas.width;
ch = ctx1.canvas.height;
sim = setInterval( function() {
progressSim(ctx1);
}, 50 );
setTimeout(function () {
i++;
if (i < allChild.length) {
loop();
}
}, 20)
}
loop();
function progressSim(ctx){
diff = ((al / 100) * Math.PI*2*10).toFixed(2);
ctx.clearRect(0, 0, cw, ch);
ctx.lineWidth = 15;
ctx.fillStyle = '#09F';
ctx.strokeStyle = "#09F";
ctx.textAlign = 'center';
ctx.fillText(al+'%', cw*.5, ch*.5+2, cw);
ctx.beginPath();
ctx.arc(100, 85, 75, start, diff/10+start, false);
ctx.stroke();
if(al >= 30){
clearTimeout(sim);
}
al++;
}
Let me know if any concerns. 让我知道是否有任何问题。 Thank you in advance. 先感谢您。
Well, al
is undefined in progressSim
. 好吧, al
在progressSim
是未定义的。 How do you ever expect it to ever become >= 30
? 您如何期望它变成>= 30
?
Adding something similar to var al
at the beginning of your script should help. 在脚本的开头添加类似于var al
的内容应该会有所帮助。
Tried clearInterval(sim)
? 尝试clearInterval(sim)
吗? You are trying to clear a timeout sim
that doesn't exist 您正在尝试清除不存在的超时sim
Also you are creating multiple sim
intervals by calling the loop function in the timeout again 您还可以通过再次在超时中调用循环函数来创建多个sim
间隔
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.