javascript - clearTimeout on dynamically updating timer -
i've got timer i'm updating dynamically.
------------------update -------------------------- when first posted question, didn't think mattered timer being called within backbone view, believe result of that, can't use global variable (or @ least global variable isn't working). i'll calling multiple timers, setting 1 global variable , deleting won't work. need able clear single timer without clearing others.
what start timer,
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(); = setinterval(this.rewritecounter,1000); }
in app, initiate timer in backbone view
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; // 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(); } });
so i'm trying update timer, kill old timer, , restart new values. have different timers, calling timed.length
within countdown function won't work.
var main_timer = settimeout(new countdown(timed.length, 'main_timer'),timed_length*1000);
this statement creates local variable main_timer
. instead have create global variable , use clear time out shown below
cleartimtout(main_timer); main_timer = settimeout(new countdown(timed.length, 'main_timer'),timed_length*1000);
edit:
use function settimeout
handler shown below
cleartimeout(main_timer); main_timer = settimeout(function(){ new countdown(timed.length, 'main_timer'); },timed_length*1000);
note: hope timed.length
, timed_length
correct.
edit:
modify countdown
given below.
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(); = setinterval(this.rewritecounter,1000); return this; }
and in 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(); } });
Comments
Post a Comment