Javascript custom sleep function

Submitted by metaharper - 5 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){

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