JS - Timers

by Ryan Morris

JavaScript

// Set up
function callMe(x) {
    console.log("Hi there", x);
}

// 
// Example 1 - setTimeout
//

// wrong - can you see why?
var timer = setTimeout(callMe(5), 1000);

// right
/*var timer = setTimeout(function() {
    callMe(5);
}, 1000);/**/

// clearTimeout(timer);

//
// Example 2 - setInterval
//

// wrong - can you see why?
//var interval = setInterval(callMe, 3000);

// right
/*
var interval = setInterval(function() {
  callMe(5);
}, 3000);/**/

//clearInterval(interval);