jQuery Timer Plugin

Posted under jQuery on June 14, 2009.

A modified version of my jQuery Delay Plugin, this plugin allows you to set timers that fire an event after a certain amount of time. The main difference between this and the jQuery Delay Plugin is this allows you to remove a timer once it has been set. Here is how you use it:

$(document).ready(function(){

  // This will hold our timer
  var myTimer = {};

  // Wait for #start to be clicked...
  $("#start").click(function(){

    // Set the timer for 2 seconds
    myTimer = $.timer(2000,function(){

      // Display hello message when timer goes off
      alert('A Delayed Hello!');


    // Optional function to call when timer is canceled
    },function(){
	
      // Display new message when canceled
      alert('Hello Message Canceled!');
	
    });

  });


  // If #cancel is clicked cancel the timer
  $('#cancel').click(function(){

    $.clearTimer(myTimer);

  });

});

Demo   Download

Comment

See what others have to say on this topic, or add your own two cents.