Question

Like title says, how to wait, let’s say 5 seconds, to execute the next line in javascript?

Answer

First, create a wait function to freeze the code:

1
2
3
4
5
6
7
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}

Insert wait function into your code, the paramemter is millisecond.

1
2
3
console.log('before');
wait(5000);
console.log('after');

Reference


This is the end of post