Javascript custom sleep function

Submitted by deeperdata - 4 years ago

I've had to use this in a number of user experience settings. Define the function and then trigger the function (along with the number of seconds you would like your script to pause - represented in milliseconds).

// Define the function
function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

// Now you can tell Javascript to take a nap whenever you would like by issuing the following:
sleep(2000); 
affiliate_link
Learn PHP
comments powered by Disqus
Proudly hosted on Digital Ocean