2011-12-19 107 views
1

我有這樣的代碼,使圖像動畫,但我希望在動畫是通過調用clearTimeout(gLoop);調用setTimeout函數之後的函數

var Animate = function(){ 
    Clear(); 
    MoveDown(); 
    gLoop = setTimeout(Animate,40); 
} 

var MoveDown = function(){ 
    // animation code 
    if(velocity==0){ 
     clearTimeout(gLoop); 
     AnotherAction(); //Here is not working 
    } 
} 

在哪裏我應該做的通話結束後要調用的函數AnotherAction()AnotherAction()

回答

3

我認爲問題在於,您正在清除之前的之後的下一次設置。 MoveDown正在清除超時,但只要控制權切換回Animate,您就再次設置它。

嘗試這樣:

var Animate = function(){ 
    Clear(); 
    if (MoveDown()) 
     gLoop = setTimeout(Animate,40); 
} 

var MoveDown = function(){ 
    // animation code 
    if(velocity==0){ 
     AnotherAction(); //Here is not working 
     return false; 
    } 
    return true; 
} 
+1

我不明白,爲什麼會'clearTimeout()'另一個函數調用之前不允許調用該函數? – 2011-12-19 05:48:26

+0

@JaredFarrish - 它會允許調用其他函數,但我認爲問題在於調用Animate的原始setTimeout仍然被解僱,這是他的結果搞砸了。 – 2011-12-19 05:49:27

+0

Ohhhhhh,我明白了。 – 2011-12-19 05:51:13