/ JAVASCRIPT, FRONT-END

How to wait few seconds to execute the next line in Javascript?

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:

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.

console.log('before');
wait(5000);
console.log('after');

Reference


This is the end of post