Created
September 17, 2013 23:13
-
-
Save pfiller/6601992 to your computer and use it in GitHub Desktop.
Don't rely on setTimeout if you need something to happen at a specific time. Putting a computer to sleep delays the execution of the timeout for the entire length of sleep (at least in Chrome).
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>SetTimeout + Computer Sleep Demo</title> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
var now = new Date(); | |
var timeout = 10000; | |
console.log("Timeout set at ", now); | |
setTimeout(function(){ | |
var then = new Date(); | |
diff = (then-now-timeout)/1000; | |
console.log("Timeout function called at ", then); | |
console.log("~~~~") | |
if(diff > 1){ | |
console.log("Your computer was asleep for ", Math.round(diff), " seconds") | |
} | |
else{ | |
console.log("Your computer was not asleep"); | |
} | |
}, timeout); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment