Tuesday, December 4, 2012

Namespacing in Javascript Classes

I\’m trying to create a timer in Javascript and I have a specific issue with how I\’m implementing it.


Right now it\’s like this



function CountUpTimer(seconds,name,targetId,submitButtonId){
this.time = seconds;
this.currentTime = 0;
this.minutes = Math.floor(seconds/60);
this.submitButtonId = submitButtonId;

this.seconds = seconds - this.minutes*60;
this.currentSeconds = 0;
this.currentMinutes = 0;
this.targetId = targetId;
this.name = name;
this.isPaused = false;
this.init = function(){
setInterval(this.name + \".tick()\",1000);
}

this.pause = function(){
this.isPaused = true;
}
this.unpause = function(){
this.isPaused = false;
}
this.tick = function(){
if(this.isPaused == false){
if(this.currentTime <= this.time){
if(this.currentSeconds == 59){
this.currentSeconds = 0;
this.currentMinutes++;
}
this.updateTimer();
this.currentTime++;
this.currentSeconds++;
} else{
this.endTiming();
}
}
}


Now, the problem with this is that I can\’t dynamically create CountUpTimer objects, because I need to know the name of the variable that I am assigning to that object. Is there some way I can work around this – so let\’s say something like



setInterval(this.tick(),1000);


?






Rating: 1 out of 5 based on 3 ratings



The post Namespacing in Javascript Classes appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/namespacing-in-javascript-classes.html

No comments:

Post a Comment