8

I want to force a function to wait 1 second before she continues after to do that in javascript . For example:

function DoSomething(){
// wait 1 second
//continue w.e is this function does.
}

Thanks.

Ori Refael
  • 2,888
  • 3
  • 37
  • 68

1 Answers1

15

If i understand you right , you need this:

setTimeout(DoSomething ,1000);

Edit , thanks to Teemu:

function DoSomething (){
    setTimeout(ContinueDoSomething ,1000);
}
function ContinueDoSomething (){
    // ...
}

And even one more way , may be most effective in some cases:

function DoSomething (){
    setTimeout( function(){
        // ...
    },1000);
}
Community
  • 1
  • 1
Ivan Chernykh
  • 41,617
  • 13
  • 134
  • 146