2021年2月5日星期五

pass value from span tag to input tag

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">
https://stackoverflow.com/questions/66072049/pass-value-from-span-tag-to-input-tag February 06, 2021 at 07:39AM

没有评论:

发表评论