This article example shares the specific code of JS to implement the stopwatch timer for your reference. The specific content is as follows Implementation of stopwatch timer: The effect diagram is as follows: Attached code, debugged and run <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> #div1 { width: 300px; height: 400px; background: skyblue; margin: 100px auto; text-align: center; } #count { width: 200px; height: 150px; line-height: 150px; margin: auto; font-size: 40px; } #div1 input { width: 150px; height: 40px; background: orange; font-size: 25px; margin-top: 20px } </style> </head> <body> <div id="div1"> <div id="count"> <span id="id_H">00</span> <span id="id_M">00</span> <span id="id_S">00</span> </div> <input id="start" type="button" value="Start"> <input id="pause" type="button" value="Pause"> <input id="stop" type="button" value="Stop"> </div> <script> //You can simplify the operation of finding label nodes var btn = getElementById('btn') function $(id) { return document.getElementById(id) } window.onload = function() { //Click to start building and counting var count = 0 var timer = null //The timer variable records the return value of the timer setInterval $("start").onclick = function() { timer = setInterval(function() { count++; console.log(count) // Need to change the value of hours, minutes and seconds on the page console.log($("id_S")) $("id_S").innerHTML = showNum(count % 60) $("id_M").innerHTML = showNum(parseInt(count / 60) % 60) $("id_H").innerHTML = showNum(parseInt(count / 60 / 60)) }, 1000) } $("pause").onclick = function() { //Cancel the timer clearInterval(timer) } //Stop counting, clear data, and clear page display data$("stop").onclick = function() { //Cancel the timer $("pause").onclick() // clearInterval(timer) //Data clear total seconds clear count = 0 // Page display data cleared $("id_S").innerHTML = "00" $("id_M").innerHTML = "00" $("id_H").innerHTML = "00" } //Encapsulate a function that processes single-digit numbers function showNum(num) { if (num < 10) { return '0' + num } return num } } </script> </body> </html> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: mysql5.7.18.zip Installation-free version configuration tutorial (windows)
>>: Detailed explanation of Linx awk introductory tutorial
Record the installation and configuration method ...
Table of contents 1. Database design 2. Front-end...
1. Use the installation package to install MySQL ...
1. Preparation 1.1 harbor download harbor downloa...
First, let’s take an example: There is a type fie...
When we check the source code of many websites, w...
1. InnoDB storage engine must be used It has bett...
background During the project development process...
The Linux operating system has revolutionized the...
Concurrency Functions time for i in `grep server ...
1. Use curl command to access by default: # curl ...
Table of contents Preface Problem: Large file cop...
1. forEach() is similar to map(). It also applies...
Table of contents Preface 1. Props, $emit one-way...
This article example shares the specific code of ...