2012-05-17 29 views
4

當clearInterval()不停止它時,如何停止計時器?Javascript setTimeout不停在clearInterval()

此代碼的目的是從0開始向上動畫一個數字,直到到達最後(例如從0 ... 75%的動畫)。但是,當我調用clearInterval定時器不停止():

http://jsfiddle.net/pwYEe/2/

<div id="foo"></div> 
<div id="bar"></div> 

animate("99%", $("#foo")); // doesnt stop 
animate("75%", $("#bar")); // doesnt stop 

function loop(clr, clr2, ele, rand, last, delay) { 
    clearInterval(clr); 
    clearInterval(clr2); 
    inloop(clr, clr2, ele, rand, last, delay); 

    if (rand >= last) { 
     clearInterval(clr); 
     clearInterval(clr2); 
    } 
    else { 
     clr2 = setTimeout(function() { 
      loop(clr, clr2, ele, rand, last, delay); 
     }, 2500); 
    } 

} 
function inloop(clr, clr2, ele, rand, last, delay) { 
    ele.html((rand += 1) + "%"); 

    if (rand >= last) { 
     clearInterval(clr); 
     clearInterval(clr2); 
    } 
    else { 
     clr = setTimeout(function() { 
      inloop(clr, clr2, ele, rand, last, delay); 
     }, delay); 
    } 
} 

function animate(end, display) { 
    var clr = null; 
    var clr2 = null; 
    var ele = null; 
    var rand = 0; // initial number 
    var last = 99; // last number 
    var delay = 5; // inner loop delay 

    ele = display; 
    ele.html("0%"); 

    var idx = end.indexOf("%"); 
    if (idx >=0) { 
     end = end.substring(0,idx); 
    } 
    last = parseInt(end); 
    loop(clr, clr2, ele, rand, last, delay); 
} 

回答

10

您使用clearTimeout()setTimeout()

您不要使用clearInterval()setTimeout()clearInterval()結合setInterval()

您的代碼中也存在結構性問題。你正在繞過clr和clr2作爲參數,並期望那些被修改的原始值不是。

您可以修復,通過把他們都在一個對象,並傳遞對象是這樣的:

animate("99%", $("#foo")); 
//animate("75%", $("#bar")); 

function loop(timers, ele, rand, last, delay) { 
    clearTimeout(timers.clr); 
    clearTimeout(timers.clr2); 
    inloop(timers, ele, rand, last, delay); 

    if (rand >= last) { 
     clearTimeout(timers.clr); 
     clearTimeout(timers.clr2); 
    } 
    else { 
     timers.clr2 = setTimeout(function() { 
      loop(timers, ele, rand, last, delay); 
     }, 2500); 
    } 

} 
function inloop(timers, ele, rand, last, delay) { 
    ele.html((rand += 1) + "%"); 

    if (rand >= last) { 
     clearTimeout(timers.clr); 
     clearTimeout(timers.clr2); 
    } 
    else { 
     timers.clr = setTimeout(function() { 
      inloop(timers, ele, rand, last, delay); 
     }, delay); 
    } 
} 

function animate(end, display) { 
    var timers = {clr: null, clr2: null}; 
    var ele = null; 
    var rand = 0; // initial number 
    var last = 99; // last number 
    var delay = 5; // inner loop delay 

    ele = display; 
    ele.html("0%"); 

    var idx = end.indexOf("%"); 
    if (idx >=0) { 
     end = end.substring(0,idx); 
    } 
    last = parseInt(end); 
    loop(timers, ele, rand, last, delay); 
} 
​ 

工作的jsfiddle:http://jsfiddle.net/jfriend00/PEqeY/

+0

大聲笑。我只是以這個問題爲例,它使用clearInterval:http://stackoverflow.com/a/4424211/325727。讓我相信我在互聯網上閱讀的任何內容。現在看起來不錯http://jsfiddle.net/pwYEe/4/ –

+0

更正 - 那jsFiddle仍然無法正常工作。達到99%後,再次從0重新開始。不知道爲什麼 –

+0

@JK - 請描述jsFiddle應該做什麼? – jfriend00