Javascript sleep : Miscellaneous Functions : PHP functions in JavaScript JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » PHP functions in JavaScript » Miscellaneous Functions »

 

Javascript sleep


Delay for a given number of seconds
Example 1

Running
1.sleep(1);

Could return
1.0
function sleep(seconds) {
    // Delay for a given number of seconds  
    // 
    // version: 901.2514
    // discuss at: http://phpjs.org/functions/sleep
    // +   original by: Christian Doebler
    // +   bugfixed by: Brett Zamir
    // %          note: For study purposes. Current implementation could lock up the user's browser. 
    // %          note: Consider using setTimeout() instead.
    // *     example 1: sleep(1);
    // *     returns 1: 0
    
    var start = new Date().getTime();
    while (new Date() < start + seconds*1000);
    return 0;
}


HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo PHP functions in JavaScript
» Miscellaneous Functions