我聲明瞭一個全局變量sim,其中我初始化了setInterval。 progressSim函數我使用了clearTimeout(),但它不會停止setInterval。setInterval在clearTimeout被調用後繼續運行
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++;
}
讓我知道是否有任何顧慮。先謝謝你。
存在
而且要創建多個
sim
區間,我認爲clearTimeout和clearInterval是可以互換的,但至少你一致性應該使用適當的「對應」。你到底做了什麼來調試呢?你有沒有f.e.檢查它是否真的進入,如果? – CBroe好吧,你通過多次調用'loop'來啓動多個'setInterval's,但只有最近的一個('sim'參考)纔會被清除。 – Bergi