Call a function after SetTimeout function

I have this code that make an image animation, but I want to call the function AnotherAction() when the animation is finished through the call clearTimeout(gLoop);

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

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

Where I supposed to make the call to AnotherAction()?

Answers


I think the problem is that you're clearing the timeout before you're setting it the next time. MoveDown is clearing the timeout, but as soon as control switches back to Animate, you're setting it again.

Try something like this:

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;  
}

Need Your Help

How do I remove the itunes published languaged set to english?

iphone email local

The app is for french and in french but when developed, I started with a shell with an english locale only and filled in the english locale with french translations...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.