2012-03-10 1 views
0

私は動的に更新しているタイマーを持っています。timerを動的に更新する際のclearTimeout

------------------更新-------------------------- 最初に質問を投稿したときに、タイマーがバックボーンビューから呼び出されているとは思えませんでしたが、その結果、グローバル変数(または少なくともグローバル変数動作していません)。私は複数のタイマーを呼び出すので、グローバル変数を1つだけ設定して削除することはできません。私は、他のものをクリアすることなく1つのタイマーをクリアすることができる必要があります。

私はタイマーを開始、私のアプリで

 
function countDown(end_time, divid){ 
    var tdiv = document.getElementById(divid), 
     to; 
    this.rewriteCounter = function(){ 


     if (end_time >= MyApp.start_time) 
     { 


     tdiv.innerHTML = Math.round(end_time - MyApp.start_time); 
     } 
     else { 
     alert('times up'); 
     } 
    }; 
    this.rewriteCounter(); 
    to = setInterval(this.rewriteCounter,1000); 
} 

、私は

 
MyApp.Views.Timer = Backbone.View.extend({ 
el: 'div#timer', 

initialize: function(){ 
    timer = this.model; 
    this.render(); 
}, 
events: { 
    "clicked div#add_time": "update_timer" 
} 

render: function(){ 
    $(this.el).append(HandlebarsTemplates['timer'](timer); 
    this.start_timer(); 
}, 
start_timer: function(){ 
    delete main_timer; // this doesn't work :(
    clearTimtout(main_timer); //this doesn't work either :(

    var main_timer = setTimeout(new countDown(timed.length, 'main_timer'),timed.length*1000); 
}, 

update_timer: function(){ 
    timed.length=timed.length+30 
    this.start_timer(); 
} 
}); 

とバックボーンビューでタイマーを開始するので、私がやろうとしていることは、タイマーを更新することです古いタイマーを終了し、新しい値で再起動します。私は異なるタイマーを持っているので、カウントダウン機能内でtimed.lengthを呼び出すだけでは機能しません。

+0

であなたは 'のsetTimeout(から返された値を保持するために、グローバル変数を必要とすることに注意してください)'。 'setTimeout()'によって返された値でdeleteを呼び出すことはできません。また、 'clearTimtout()'で行ったスペルミスに注意してください。さらに、関数が存在した後に値が保存されない 'start_timer()'関数でmain_timerをローカル変数として再宣言しています。 – Yaniro

+0

ありがとうYaniro、私は質問を更新しました。 – pedalpete

答えて

2
var main_timer = setTimeout(new countDown(timed.length, 'main_timer'),timed_length*1000); 

このステートメントは、ローカル変数main_timerを作成します。

使用setTimeoutハンドラとしての機能

clearTimeout(main_timer); 
main_timer = setTimeout(function(){ 
    new countDown(timed.length, 'main_timer'); 
},timed_length*1000); 

ノートの下に示すように:代わりに、グローバル変数を作成し、

clearTimtout(main_timer); 
main_timer = setTimeout(new countDown(timed.length, 'main_timer'),timed_length*1000); 

EDIT下に示すように、時間をクリアするためにそれを使用する必要があります。 timed.lengthtimed_lengthが正しいことを願います。

EDIT:

下記のようcountdownを変更します。 `てclearTimeout()`以降を使用するために

function countDown(end_time, divid){ 
    var tdiv = document.getElementById(divid), 
     to; 
    this.rewriteCounter = function(){ 


     if (end_time >= MyApp.start_time) 
     { 


     tdiv.innerHTML = Math.round(end_time - MyApp.start_time); 
     } 
     else { 
     alert('times up'); 
     } 
    }; 

    this.clearRewriteCounter = function(){ 
     clearInterval(to); 
    } 

    this.rewriteCounter(); 
    to = setInterval(this.rewriteCounter,1000); 

    return this; 
} 

とMyApp.Views.Timer

MyApp.Views.Timer = Backbone.View.extend({ 
el: 'div#timer', 

initialize: function(){ 
    timer = this.model; 
    this.render(); 
}, 
events: { 
    "clicked div#add_time": "update_timer" 
} 

render: function(){ 
    $(this.el).append(HandlebarsTemplates['timer'](timer); 
    this.start_timer(); 
}, 
start_timer: function(){ 
    clearTimeout(this.main_timer); 
    this.main_timer = setTimeout(function(){ 
     if(this.countDownInstance){ 
      this.countDownInstance.clearRewriteCounter(); 
     } 
     this.countDownInstance = new countDown(timed.length, 'main_timer'); 
    },timed_length*1000); 
}, 

update_timer: function(){ 
    timed.length=timed.length+30 
    this.start_timer(); 
} 
}); 
+0

Diodeさん、ありがとうございました。グローバル変数を使用しようとしましたが、質問に記載されているように、複数のタイマーがあるため、グローバル変数が正確に働いていません。私が言及しなかったのは、タイマーがbackbone.jsビューの中から呼び出されているということでした。なぜそれがグローバル変数へのアクセス権を持っていないのかもしれませんが、私は完全にはわかりません。 – pedalpete

+0

質問を通過した後、問題の内容を理解しました。更新された回答をご覧ください。 – Diode

関連する問題