Jump to content

[SOLVED] setTimeout problem in javascript class


Darghon

Recommended Posts

Hello, i'm trying to create a js class that create a timer, and performs an action or a countdown every interval

so far it only does the tick procedure once, then it sais that the tick function does not exist

 

please help

/* This will be the timer function */
function Timer(action,interval,countdown){
this.active = false;
this.action = action || false;
this.interval = interval || 100; //default interval at 100 ms
this.timerID = false;
this.countDown = countdown || -1;
}

Timer.prototype.getActive = function(){ return this.active; }
Timer.prototype.setActive = function(active){ this.active = active; }
Timer.prototype.getAction = function(){ return this.action; }
Timer.prototype.setAction = function(action){ this.action = action; }
Timer.prototype.getInterval = function(){ return this.interval; }
Timer.prototype.setInterval = function(interval){ this.interval = interval; }
Timer.prototype.getCountDown = function(){ return this.countDown; }
Timer.prototype.setCountDown = function(countdown){ this.countDown = countdown; }
Timer.prototype.start = function(){ this.active = true; this.tick(); }
Timer.prototype.stop = function(){ this.active = false; clearTimeout(this.timerID); }
Timer.prototype.tick = function(){
if(this.countDown == 0){
	if(this.action != ""){
		eval(this.action);
	}
	this.stop();
}
else{
	if(this.countDown > -1){
		//countdown is active
		this.countDown -= (this.interval/1000); //Remove interval from countdown
	}
	else{
		if(this.action != ""){
			eval(this.action);
		}
	}
	this.timerID = window.setTimeout(this.tick,this.interval);
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.