JavaScript Timer | JS Timer | JavaScript Countdown Timer By WDH

JavaScript Timer

Javascript Timer
Timer events
setTimeout(function, delayMS); 
arranges to call given function after given delay in ms 
setInterval(function, delayMS); 
arranges to call function repeatedly every delayMS ms 
stops the given timer so it will not call its function 
Note : this ID can be passed to clearTimeout/Interval later to stop the timer
After clicking the button Timer start now
Passing parameters to timers
function delayedMultiply() {
// 6 and 7 are passed to multiply when timer goes off
setTimeout(multiply, 2000, 6, 7);
function multiply(a, b) {
alert(a * b);
  • any parameters after the delay are eventually passed to the timer function
  • Why not just write this? 
setTimeout(multiply(6 * 7), 2000);
Common timer errors
setTimeout(booyah(), 2000);
setTimeout(booyah, 2000);
setTimeout(multiply(num1 * num2), 2000);
setTimeout(multiply, 2000, num1, num2);
  • what does it actually do if you have the () ?
  • it calls the function immediately, rather than waiting the 2000ms!