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);
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.