1 // source/credits: "Algorithm": http://www.codingforums.com/showthread.php?s=&threadid=10531
2 // The constructor should be called with
3 // the parent object (optional, defaults to window).
6 this.obj = (arguments.length)?arguments[0]:window;
10 // The set functions should be called with:
11 // - The name of the object method (as a string) (required)
12 // - The millisecond delay (required)
13 // - Any number of extra arguments, which will all be
14 // passed to the method when it is evaluated.
16 Timer.prototype.setInterval = function(func, msec){
17 var i = Timer.getNew();
18 var t = Timer.buildCall(this.obj, i, arguments);
19 Timer.set[i].timer = window.setInterval(t,msec);
22 Timer.prototype.setTimeout = function(func, msec){
23 var i = Timer.getNew();
24 Timer.buildCall(this.obj, i, arguments);
25 Timer.set[i].timer = window.setTimeout("Timer.callOnce("+i+");",msec);
29 // The clear functions should be called with
30 // the return value from the equivalent set function.
32 Timer.prototype.clearInterval = function(i){
33 if(!Timer.set[i]) return;
34 window.clearInterval(Timer.set[i].timer);
37 Timer.prototype.clearTimeout = function(i){
38 if(!Timer.set[i]) return;
39 window.clearTimeout(Timer.set[i].timer);
45 Timer.set = new Array();
46 Timer.buildCall = function(obj, i, args){
48 Timer.set[i] = new Array();
50 Timer.set[i].obj = obj;
51 t = "Timer.set["+i+"].obj.";
55 Timer.set[i][0] = args[2];
56 t += "Timer.set["+i+"][0]";
57 for(var j=1; (j+2)<args.length; j++){
58 Timer.set[i][j] = args[j+2];
59 t += ", Timer.set["+i+"]["+j+"]";
62 Timer.set[i].call = t;
65 Timer.callOnce = function(i){
66 if(!Timer.set[i]) return;
67 eval(Timer.set[i].call);
70 Timer.getNew = function(){
72 while(Timer.set[i]) i++;