Archive for the ‘javascript’ Category

Sleeping with Javascript

June 07, 2009 By Hel :: Posted in javascript :: No comments

When I went looking for a built-in “sleep()” function in Javascript, I quickly learned that it does not exist. This is surprising considering that “sleep()” is in all other languages that I have used. To get around this javascript shortcoming, I decided to create my own sleep function. The result is a function that loops internally until the number of seconds passed is greater than the argument value, “naptime”.

Initial tests of “sleep()” were successful, however, I soon discovered that most browsers will interrupt “sleep()” and show a “Slow/Busy script” dialog message whenever the “naptime” argument is greater than 10 seconds (7 seconds in IE). Trying to work around this problem was too much effort, so I left the script as is. If you use this function, I recommend a “naptime” of less than 10 seconds so your site visitors won’t encounter this “slow script” dialog message.

Below are some examples of the javascript “sleep()” function. Just click on a link below to start sleeping for the preset amount of seconds shown.

Read the rest of this entry »