jquery - Timers in javascript don't want to stop counting sometimes -
i have 3 timers in javascript , created buttons control flow (switch them). (only!) don't want stop. can problem? timers? testing on google chrome.
so, important parts of code:
//to insert infos function addinfo(info){ $("#info").text(info); cleartimeout(window.mytimer); window.mytimer = settimeout(function() { $("#info").text(""); }, 5000); return true; } function starthtml(){ window.timerhtml = setinterval(function(){ //ajax }, 500); } function startview(){ window.timerview = setinterval(function(){ //ajax }, 2000); } function stop_html(){ clearinterval(timerhtml); startview(); addinfo("html->view "); } function start_html(){ clearinterval(timerview); starthtml(); addinfo("view->html"); } $(function() { startview(); start_html(); //these actions should switch timers $("#stop_html").click(function(){ stop_html(); return false; }); $("#start_html").click(function(){ start_html(); return false; }); });
edit: added html
<button class="btn btn-primary btn-allwidth" id="start_html" data-toggle="tooltip" data-placement="right" title="view->html">html on</button> <button class="btn btn-primary btn-allwidth" id="stop_html" data-toggle="tooltip" data-placement="right" title="html->view">html off</button> <a href="#" onclick="javascript:$('#info-container').toggle('slow'); return false;" class="btn btn-primary btn-allwidth" data-toggle="tooltip" data-placement="right" title="infos">infos<i class="glyphicon glyphicon-chevron-down"></i></a> <div id="info-container"> <div id="info" class="alert alert-success"></div> </div>
if either of starthtml or startview executed in succession (i.e. starthtml executed 2 times continuously) end creating 2 intervals.
when execute
window.timerhtml = setinterval(function(){ //ajax }, 500);
twice in succession, window.timerhtml gets new interval assigned it. not mean previous interval assigned window.timerhtml prior has been cleared. still goes on no longer assigned variable accessed.
you can test running these 2 commands successively
window.timerhtml = setinterval(function(){ console.log("a"); }, 500); window.timerhtml = setinterval(function(){ console.log("b"); }, 500);
even if clear window.timerhtml still see "a" being logged in console. becuase previous timer never got cleared. , timerhtml no longer has access it.
this reason why timers wouldn't stop.
Comments
Post a Comment