2011-12-19 105 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