jquery - How to manage multiple timers in javascript? -


below coding use 1 count timer:

var sec = 0; function pad ( val ) { return val > 9 ? val : "0" + val; }        function settime() {     document.getelementbyid("seconds0").innerhtml=pad(++sec%60);     document.getelementbyid("minutes0").innerhtml=pad(parseint(sec/60,10)); }  var timer = setinterval(settime, 1000); 

if have 2 timers, write this:

var sec = 0; var sec1 = 0; function pad ( val ) { return val > 9 ? val : "0" + val; }        function settime() {     document.getelementbyid("seconds0").innerhtml=pad(++sec%60);     document.getelementbyid("minutes0").innerhtml=pad(parseint(sec/60,10)); } function settime1() {     document.getelementbyid("seconds1").innerhtml=pad(++sec1%60);     document.getelementbyid("minutes1").innerhtml=pad(parseint(sec1/60,10)); }  var timer = setinterval(settime, 1000); var timer1 = setinterval(settime1, 1000); 

actually timer use show waiting time of people. number of people unknown variable. means can 1 - 100. so, 1 people 1 timer. below function written.

showwait = function(){     var html = "";     (var = 0; < total; i++)      {                              html += '<div id="numbers" class="col-sm-3 col-xs-6">'+i+'</div>';             html += '<div id="qtime" class="col-sm-3 col-xs-6"></span><div><span class="glyphicon glyphicon-time"></span> waiting</div><div id="waittime"><label id="minutes'+i+'">00</label>:<label id="seconds'+i+'">00</label></div></div>';             html += '</div>';     } $('#waitnumber').html(html); } 

so, don't think way create more timer keep repeating same function on , on again, right? can't if 100 people there 100 timers, right? there simpler way manage multiple timers?

i guess every interval has 1000 ms waiting time, dont need multiple timers 1 , in 1 timer need every person

var sec = [time0, time1, time2 ....]; function pad ( val ) { return val > 9 ? val : "0" + val; }        function settime() {     for(person = 0; person < numberofpeople; person++)     {         document.getelementbyid("seconds" + person).innerhtml=pad(++sec[person]%60);         document.getelementbyid("minutes" + person).innerhtml=pad(parseint(sec[person]/60,10));     } } var timer = setinterval(settime, 1000); 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -