2012-05-17 3 views
4

clearInterval()이 타이머를 중지시키지 않을 때 어떻게 중지합니까?Javascript setTimeout on 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

당신은 setTimeout()clearTimeout()를 사용합니다.

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