I have a counter in span tags, when I press Start the timer starts counting and when pressing Pause it is stopped. Now the question is, How do I pass the value from the counter to input tag when Pause clicked?
<span id="min">00</span>:<span id="sec">00</span> <input id="startButton" type="button" value="Start"> <input id="pauseButton" type="button" value="Pause"> Script
<script> const Clock = { totalSeconds: 0, start: function () { if (!this.interval) { const self = this; function pad(val) { return val > 9 ? val : "0" + val; } this.interval = setInterval(function () { self.totalSeconds += 1; document.getElementById("min").innerHTML = pad(Math.floor(self.totalSeconds / 60 % 60)); document.getElementById("sec").innerHTML = pad(parseInt(self.totalSeconds % 60)); }, 1000); } }, pause: function () { clearInterval(this.interval); delete this.interval; }, }; document.getElementById("startButton").addEventListener("click", function () { Clock.start(); }); document.getElementById("pauseButton").addEventListener("click", function () { Clock.pause(); }); </script> Here it is as a runnable snippet:
const Clock = { totalSeconds: 0, start: function() { if (!this.interval) { const self = this; function pad(val) { return val > 9 ? val : "0" + val; } this.interval = setInterval(function() { self.totalSeconds += 1; document.getElementById("min").innerHTML = pad(Math.floor(self.totalSeconds / 60 % 60)); document.getElementById("sec").innerHTML = pad(parseInt(self.totalSeconds % 60)); }, 1000); } }, pause: function() { clearInterval(this.interval); delete this.interval; }, }; document.getElementById("startButton").addEventListener("click", function() { Clock.start(); }); document.getElementById("pauseButton").addEventListener("click", function() { Clock.pause(); }); <span id="min">00</span>:<span id="sec">00</span> <input id="startButton" type="button" value="Start"> <input id="pauseButton" type="button" value="Pause">
没有评论:
发表评论