简体   繁体   English

在调用clearTimeout之后,setInterval继续运行

[英]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 . 好吧, alprogressSim是未定义的。 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.

相关问题 在调用clearTimeout之后,setTimeout继续运行 - setTimeout keeps running after clearTimeout has been called setInterval 在调用 clearInterval 后继续运行 - setInterval keeps on running after clearInterval called 在 clearInterval 运行后 React Native setInterval 运行 5 或 6 次 - React Native setInterval running 5 or 6 times after clearInterval has been ran setinterval在ajax加载后保持运行 - setinterval keeps running after ajax load 在JavaScript中调用clearInterval后,如何恢复setInterval? - How can I resume setInterval after clearInterval has been called in javascript? 几秒钟后如何停止运行功能? setTimeout,clearTimeout和setInterval无法正常工作 - How to stop a function running after a number of seconds? setTimeout, clearTimeout and setInterval wont work correctly 在调用clearInterval()之后,setInterval()循环仍在运行 - setInterval() loop still running after clearInterval() is called Javascript - 在调用clearInterval()之后仍然运行setInterval() - Javascript - setInterval() still running after clearInterval() called 即使在使用 clearInterval() 清除 setInterval() 后,它也会继续运行 - setInterval() keeps running even after clearing it using clearInterval() React setInterval function 在重新渲染后继续在后台运行 - React setInterval function keeps running in the background after rerender
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM