Ik heb zelf ooit een Timer geschreven die ik erg graag gebruik. 't Bestaat uit 2 delen: Timer.js en EventManager.js. De één heeft de ander nodig voor functioneren
Hieronder nog:
- Voorbeeld van gebruik (of zie demo:
http://robins.awardspace.com/TimerTester/)
- Timer.js
- EventManager.js
Hoop dat je hier iets aan hebt

Groetjes, Robin
Voorbeeld van gebruik:
[JS]//var timer = new Timer(1000, 30); //Iedere seconde, 30 keer
var timer = new Timer(500); //Iedere halve seconde, voor altijd
timer.addEventListener("timer", function(e) {
console.log(timer.getCount());
/* Do something */
}, false);
timer.stop(); //Pauzeert de timer
timer.reset(); //Stopt en reset de timer
timer.start(); //Start de timer[/JS]
Timer.js:
[JS]/**
* @require EventManager EventManager.js
*/
function Timer (delay, repeatCount) {
EventManager.createEventDispatcher(this);
var count = 0;
this.timerId = null;
this.running = false;
this.repeatCount = repeatCount;
this.start = function () {
var self = this;
if (this.repeatCount === undefined || count < this.repeatCount) {
this.timerId = setInterval(function () {
if (self.repeatCount !== undefined && self.repeatCount - 1 <= count) {
self.stop();
}
count++;
self.dispatchEvent("timer");
}, delay);
}
this.running = true;
};
this.reset = function() {
this.stop();
count = 0;
};
this.stop = function () {
if (this.timerId !== null && this.running) {
clearInterval(this.timerId);
this.timerId = null;
this.running = false;
}
};
this.getCount = function() {
return count;
};
}[/JS]
EventManager.js:
[JS]var EventManager = (function () {
function createEventDispatcher(obj) {
obj.isEventDispatcher = true;
obj.listeners = {};
obj.addEventListener = function (type, callback, capture, priority) {
priority = priority != undefined ? priority : 0;
var typeArray = obj.listeners[type];
var listener = new Listener(callback, priority);
if (!typeArray) {
obj.listeners[type] = [listener]; //Assign to object
}
else {
var i = 0;
while (typeArray
&& typeArray.priority <= priority) {
i++;
}
obj.listeners[type].splice(i, 0, listener);
}
};
obj.removeEventListener = function (type, callback, capture) {
var typeArray = obj.listeners[type];
for (var i = 0, l = typeArray.length; i < l; i++) {
if (typeArray.callback == callback) {
obj.listeners[type].splice(i, 1);
}
}
};
obj.suspendEventListener = function (type, callback, capture) {
var typeArray = obj.listeners[type];
for (var i = 0, l = typeArray.length; i < l; i++) {
if (typeArray.callback == callback) {
typeArray.suspension++;
}
}
};
obj.dispatchEvent = function (type, args) {
var typeArray = obj.listeners[type];
if (!typeArray) {
return;
}
var e = new CustomEvent(type, obj, args);
for (var i = 0; i < typeArray.length; i++) {
if (typeArray.suspension == 0) {
typeArray.callback.call(obj, e);
// typeArray.dispatch(args);
}
else {
typeArray.suspension--;
}
}
};
}
return {
createEventDispatcher: createEventDispatcher
};
})();
function Listener(callback, priority) {
this.callback = callback;
this.priority = priority !== undefined ? priority : 0;
this.suspension = 0
}
function CustomEvent (type, target, args) {
this.type = type;
this.target = target;
for (var p in args) {
if (!this.hasOwnProperty(p)) {
this.__defineGetter__(p, function() {
return args[p];
});
}
}
this.toString = function () {
return "[object CustomEvent]";
};
}[/JS]