Darghon Posted May 27, 2009 Share Posted May 27, 2009 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 https://forums.phpfreaks.com/topic/159853-solved-settimeout-problem-in-javascript-class/ Share on other sites More sharing options...
Ken2k7 Posted May 27, 2009 Share Posted May 27, 2009 When setTimeout evaluates a code as the first parameter, it has no reference to "this" being the Timer. So you'll have to give it a Timer instance to run on. Link to comment https://forums.phpfreaks.com/topic/159853-solved-settimeout-problem-in-javascript-class/#findComment-843239 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.