Here is a little JavaScript Class which allows you to have timed function calls. Of course there could be different instances of these timers. Here is a example how to use it:

var timer = new Timer( {'interval':1000} );
timer.start( timedFunction );
 
function timedFunction(){
   // called every 1000 milliseconds
}

The function timedFunction will be called every 1000 milliseconds. The millisecond interval will be configured within the configuration object, which is given as parameter into the the class constructor. There is also an optional second parameter for the start function to set the scope. This allows you to use the timer within other JavaScript objects. To stop the timer use the stop function.

/** ------------------------------------------------------------------------------------------------------------ *
* @class: Timer
* @classDescription: implements a timer class to handle timed events
* @author: Sebastian Martens
** ------------------------------------------------------------------------------------------------------------ */
function Timer( obj ){
this.isRunning = false; // is timer currently running
this.interval = 500; // default interval of event calls
 
this.funcRef = null; // reference to called function
this.scope = this;
 
this._timerId = 0; // global timer id
 
/**
* @constructor
* @param {Object} obj - construction parameter
*/
this.construct = function( obj ){
this.mixIn( obj, this );
// find smallest not used timer id
while( document[ 'myTimer'+this._timerId ]!=null ) this._timerId++;
document[ 'myTimer'+this._timerId ] = this;
}
 
/**
* mix a given object into this object
* @param {Object} obj - given object with parameters
*/
this.mixIn = function( obj, scope ){
if(!scope) scope = this;
var item = null;
for(item in obj){
scope[ item ] = obj[item];
}
}
 
/**
* starts timer
* @param {Int} interval - time in milliseconds for time interval
* @param {Node} scope - the execution scope of the reference function
* @param {Function} funcRef - function reference of function to be called on time event
*/
this.start = function( funcRef, scope, interval ){
if(interval) this.interval = interval;
 
this.scope = scope;
this.funcRef = funcRef;
this.isRunning = true;
this.startTimer();
}
 
/**
* starts a new time event call with given time interval
*/
this.startTimer = function(){
if( !this.isRunning ) return;
window.setTimeout("document['myTimer"+this._timerId+"'].timedHandler()",this.interval);
}
 
/**
* stopps the timer
*/
this.stopp = function(){
this.isRunning = false;
}
 
/**
* timed event handler. will be called on each time event
*/
this.timedHandler = function(){
if( this.isRunning ){
if( this.funcRef ){
this.funcRef.apply(this.scope);
}else this.stopp();
 
// do next timer call
this.startTimer();
}
}
 
// constructor call
this.construct( obj );
}

Download JavaScript Timer.js

cheers,
Sebastian

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.