jQuery Timers

jQuery timers has been moved to the jQuery plugin repository where updates, bug tracking and documentation can be centralized. Please go there to ensure you have the newest version of the code.

jQuery timers is an attempt to combine jQuery's concise chaining programming style with the awkward style in which timed events are coded in JavaScript to produce a friendlier and more intuitive timed event system. In addition, it fixes the most prevalent issue with the various event systems of JavaScript: misconfigured scope. All the methods you design to use in setTimeout/setInterval in JavaScript cannot use 'this' to reference the desired element because 'this' always references the window object. Because of this, closures are required to produce useful code. Without the need for closures, these methods could be stored inside the class rather than inside the calling method which would produce a more modular and object oriented style of coding.

Features

In addition to fixing the main tangles of tangoing with timed events, this plugin adds additional features.

Abstracted Timer IDs

When you call setTimeout or setInterval with the intention of possibly stopping it later on you need to save the reference to the timer id. This is really cumbersome especially when you want loosely coupled activation deactivation of events. This plugin fixes that by storing the timer id internally and using it behind the scenes when stopping events.

Labelled Events

When you create a timed event, you have the option of giving it a label. These labels allow for multiple timed events to operate on the same object with fine control over which method you want to stop.

Limited Events

Beyond simply running once in a given amount of time and running every given amount of time, sometimes there is a need to run at a given time interval but only for a certain number of times.

String Time Parsing

Too often, people try to use setTimeout or setInterval to do something every second but forget that they both take milliseconds. jQuery timers allows you to specify your time as a string. It include the SI prfixed time values for 'ms', 'cs', 'ds', 's', 'das', 'ws', and 'ks' which may be overkill but because milliseconds and seconds were already being added, the others were trivial to implement.

Added Methods

everyTime(interval : Integer | String, [label = interval : String], fn : Function, [times = 0 : Integer])

everyTime will add the defined function (fn) as a timed event to run at a given time interval (interval) for a given number of times (times). If times is set to 0, the number of times the method is called is unbounded. A label is also set for the given timed event either to the provided string (label) or to the string representation of the interval provided. Additionally, the interval can be defined by using a string such as "3s" for 3 seconds.

oneTime(interval : Integer | String, [label = interval : String], fn : Function)

oneTime will call the defined function (fn) a certain amount of time (interval) after being added to the elements in the jQuery object. A label (label) is also set for the timed event either to the provided string (label) or to the string representation of the interval provided.

stopTime([label : Integer | String], [fn : Function])

stopTime will stop any timed events with the provided label (label) and function (fn). If neither is specified, it will stop all timed events acting on the elements in the jQuery object. If only the function is provided, then it will stop all timed events calling that function regardless of label. Finally if only the label is provided, it will stop all timed events given that label at initialization.

Licencing

This plugin is licenced with the WTFPL. In short, do whatever the fuck you want with it.

Download

jQuery timers is currently only available as unpacked javascript

Demos

Controlled everyTime

    Start | Stop

    Unbounded everyTime

    I am transient... *sigh*

    Controlled oneTime

    Click here to stop me!

    Uncontrolled oneTime

    I'm gonna change whether you like it or not.