Search code examples
javascriptjquerytimeout

execute function after another


I have this javascript code:

 if (direction !== "leftnav") { 
// DO THINGS
};
setTimeout(function () {
//DO OTHER THINGS AFTER THE FIRST THING
}, 1000);

Is it possible to execute the 2e function after the first instead of having to use a timeout event?

thanks for your help!


Solution

  • Yes it's possible. Here's an example using the only code you've provided:

    if (direction !== "leftnav") {  
        // DO THINGS...
    
        // THEN
        doOtherThings();
    
        // THEN
        doMoreThings();
    }; 
    
    var doOtherThings = function(){
        //DOING OTHER THINGS
    }
    var doMoreThings = function(){
        //DO EVEN MORE THINGS
    }