Created
October 14, 2012 02:25
-
-
Save zhuzhuaicoding/3887037 to your computer and use it in GitHub Desktop.
sleep and settimeout diff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sleep(ms) { | |
var dt = new Date(); | |
dt.setTime(dt.getTime() + ms); | |
while (new Date().getTime() < dt.getTime()); | |
} | |
// sleep(4000); | |
console.log('setTimeout begin'); | |
setTimeout( | |
function() { | |
sleep(6000); | |
console.log('0 second delay timer executed...'); | |
}, 0); | |
setTimeout(function() { | |
sleep(0); | |
console.log('4 seconds delay timer executed...'); | |
}, 4000) | |
console.log('setTimeout end'); | |
/* http://www.digimantra.com/tutorials/sleep-or-wait-function-in-javascript/ | |
The difference between the above function is that sleep will halt the script and will not allow the script to execute further lines of code. However when setTimeout is called, it won’t halt the further execution of the script, instead it will execute after the specified interval of time and also the other functions (if any) will keep on executing. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment