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
Problem description: Error message: Caused by: co...
Preface MySQL officially refers to prepare, execu...
Let’s not start with the introduction and get str...
Table of contents Browser kernel JavaScript Engin...
HTML+CSS 1. Understanding and knowledge of WEB st...
Introduction to sudo authority delegation su swit...
Table of contents 1. What is a JavaScript engine?...
This article shares the specific code of jQuery t...
This article example shares the specific code of ...
Focus images are a way of presenting content that ...
The blogger hasn't used MySQL for a month or ...
question By clicking a control, a floating layer ...
Hyperlink, also called "link". Hyperlin...
Some attributes about elements In the daily devel...
Table of contents 1. What is copy_{to,from}_user(...