This article example shares the specific code of JavaScript to implement a simple timer for your reference. The specific content is as follows <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Timer</title> <style> .bigDiv { margin: 50px auto; width: 200px; height: 200px; background-color: lightskyblue; border-radius: 10px; } #showNum { width: 200px; height: 20px; background-color: orange; text-align-all: center; border-radius: 5px; } </style> </head> <body> <div class="bigDiv"> <h2 align="center">Timer</h2> <div id="showNum" align="center"> 0 </div> <br> <br> <div class="butDiv">     <button type="button" id="start">Start</button>   <button type="button" id="stop">Stop</button>   <button type="button" id="reset">Reset</button>   </div> </div> <script> //Define the displayed time let int = null; /** * Start click event */ document.getElementById("start").addEventListener('click', function () { if (int == null) { //Set the timer//Execute every 2 milliseconds parameter 1 int = setInterval(startNum, 1000); } }); /** * Stop click event */ document.getElementById("stop").addEventListener('click', function () { // Clear the timer, clearInterval(int); int = null; }); /** * Reset click event */ let num = 0; document.getElementById("reset").addEventListener('click', function () { if (int == null) { num = 0; //Change the time to 0 document.getElementById("showNum").innerHTML = num; } }); function startNum() { num++; //Continuously change time document.getElementById("showNum").innerHTML = 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:
|
<<: Explanation of the usage of replace and replace into in MySQL
>>: Introduction to ApplicationHost.config (IIS storage configuration area file)
A set of projects for training react+ts Although ...
Preface In the process of continuous code deliver...
Preface Today, after synchronizing the order data...
Two problems that are easy to encounter when inst...
Dockerfile is a text file that contains instructi...
Table of contents Backend: Rails API part Front-e...
I reinstalled VMware and Ubuntu, but the command ...
Preface It is said that if the people doing opera...
Table of contents 1.v-model 2. Binding properties...
Recently, the company happened to be doing live b...
Table of contents Preface Computed properties Int...
Important note: Before studying this article, you...
Table of contents Preface Dockerfile What is a Do...
Table of contents 1. ChildNodes attribute travers...
Vue3 project encapsulation side navigation text s...