我試圖在Javascript中創建一個計時器,我對如何實現它有特定的問題。Javascript中的命名空間類
現在是這樣的
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();
}
}
}
現在,這個問題是我不能動態地創建CountUpTimer對象,因爲我需要知道的變量,我分配給該對象的名稱。有什麼辦法可以解決這個問題 - 所以我們來說說一下吧 -
setInterval(this.tick(),1000);
?
工作 - 謝謝 – praks5432
請注意,'Function.bind'是一個相對較新的功能,並不支持在許多瀏覽器。 – David
我爲自己創建的[Live test case](http://jsfiddle.net/weWqe/)然後意識到你是正確的。 :) –