theDeSilva.com Andrew de Silva

JavaScript Timers

August 6th, 2009 · No Comments · Javascript

JavaScript provides setTimeout(), clearTimeout() , setInterval() and clearInterval() for your javascript timer functions which allows you to run a piece of Javascript code at some point in the future. Here are some explanation of the functions.

setTimeout ( expression, timeout );

setTimeout() returns a numeric timeout ID that can be used to track the timeout. This is most commonly used with the clearTimeout() method

clearTimeout ( timeoutId );
// where timeoutId is the ID of the timeout as returned from the setTimeout() method call.
setInterval ( expression, interval );

setInterval() is really close to setTimeout() but setTimeout() only triggers the expression once while setInterval keep triggering the expression over and over.

clearInterval(timeoutId )
// where timeoutId is the ID of the timeout as returned from the setInterval() method call.

clearInterval() is used to cancel a setInterval() when you want to cancel a setInterval() then you need to call clearInterval(), passing in the interval ID returned by the call to setInterval().

Share and Enjoy:
  • Digg
  • StumbleUpon
  • del.icio.us
  • Reddit
  • Technorati
  • Facebook
  • Google Bookmarks
  • LinkedIn

Tags: ····