javascript - Two Looping Countdown Timers for Electron App -
i have javascript countdown timer trying put electron app. functions similar boxing timer. run countdown, there start/pause , reset buttons. after main countdown completes @ 00:00, code goes second countdown timer, used rest period, in between rounds. when second countdown completes, loops main round countdown. keeps looping until hit start/pause button calls cleartimeout method stop countdown. here issue arrises. start/pause , reset buttons functional while main round countdown function active.
i able have start/pause , reset buttons functional both main round , restperiod countdown functions, unsure of how functionality working on both. there way can "switch" these buttons on being functional while restperiod function running, can avoid having more buttons?
any hints, tips, replies , nudges in right direction appreciated! thank time!
var minutes = 1; var seconds = 0; var timer; var timer_is_on = 0; var resttimer; var restmin = 1; var restsec = 1; var roundmin = minutes; var roundsec = seconds; var starttone = new audio("starttone.mp3"); var stoptone = new audio("stoptone.mp3"); //main countdown function round() { roundsec--; if (roundsec === -1) { roundsec = 59; roundmin = roundmin - 1; if (roundmin < 10) { roundmin = "0" + roundmin; } } else { roundmin = roundmin; } if (roundsec < 10) { roundsec = "0" + roundsec; } document.getelementbyid("counter").style.color = "#363636"; document.getelementbyid("counter").innerhtml = roundmin + ":" + roundsec; if (roundsec == 0 && roundmin == 0) { cleartimeout(timer); roundmin = minutes; roundsec = seconds; stoptone.play(); restperiod(); } else { timer = settimeout(function() { round(); }, 1000); } } //rest period function function restperiod() { restsec--; if (restsec === -1) { restsec = 59; restmin = restmin - 1; } else { restmin = restmin; } if (restsec < 10) { restsec = "0" + restsec; } document.getelementbyid("counter").style.color = "red"; document.getelementbyid("counter").innerhtml = "0" + restmin + ":" + restsec; if (restsec == 0 && restmin == 0) { cleartimeout(resttimer); restmin = 1; restsec = 1; starttone.play(); round(); } else { resttimer = settimeout(function() { restperiod(); }, 1000); } } //start , pause button document.getelementbyid("startpause").addeventlistener("click", startpausecount); function startpausecount() { if (!timer_is_on) { timer_is_on = 1; round(); document.getelementbyid("startpause").innerhtml = "pause"; starttone.play(); } else { timer_is_on = 0; cleartimeout(timer); document.getelementbyid("startpause").innerhtml = "start"; } } //reset button document.getelementbyid("reset").addeventlistener("click", resetcount); function resetcount() { cleartimeout(timer); roundmin = minutes; roundsec = seconds; if (roundmin < 10) { roundmin = "0" + roundmin; } if (roundsec < 10) { roundsec = "0" + roundsec; } document.getelementbyid("counter").innerhtml = roundmin + ":" + roundsec; } <body> <p id="counter">01:00</p> <div id="startpause" class="button">start</div> <div id="reset" class="button">reset</div> </body>
Comments
Post a Comment