Skip to content

Instantly share code, notes, and snippets.

@pfiller
Created September 17, 2013 23:13
Show Gist options
  • Save pfiller/6601992 to your computer and use it in GitHub Desktop.
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).
<!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