强迫等待javascript

Ori*_*ael 6 javascript sleep

我想强制一个函数等待1秒,然后继续在javascript中执行此操作.例如:

function DoSomething(){
// wait 1 second
//continue w.e is this function does.
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Che*_*niv 12

如果我理解你,你需要这个:

setTimeout(DoSomething ,1000);
Run Code Online (Sandbox Code Playgroud)

编辑,感谢Teemu:

function DoSomething (){
    setTimeout(ContinueDoSomething ,1000);
}
function ContinueDoSomething (){
    // ...
}
Run Code Online (Sandbox Code Playgroud)

甚至还有一种方法,在某些情况下可能最有效:

function DoSomething (){
    setTimeout( function(){
        // ...
    },1000);
}
Run Code Online (Sandbox Code Playgroud)