Skip to content

Instantly share code, notes, and snippets.

@toanalien
Last active August 29, 2015 14:22
Show Gist options
  • Save toanalien/2a4082e099b87646bccf to your computer and use it in GitHub Desktop.
Save toanalien/2a4082e099b87646bccf to your computer and use it in GitHub Desktop.
Scheduling the Execution of Functions Using Timers
var oneSecond = 1000 * 1; // one second = 1000 x 1 ms
// setTimeout
setTimeout(function() {
document.write('<p>Hello there.</p>');
}, oneSecond);
// setInterval
var setInteval(function(){
document.write('<p>Hello there.</p>');
}, oneSecond);
// using setTimeout to defer the execution of a function
var timeout = setTimeout(function(){
console.log("Time out !");
}, oneSecond);
// using clearTimeout to cancel the execution of a function
clearTimeout(timeout);
// using setInterval to defer te repetitive of a function
var interval = setInterval(function(){
console.log("tick !");
}, 1000);
// using clearInterval to cancel the repetitive of a function
clearInterval(timeout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment