1. Brief Introduction In There are four timer method related methods:
2. setInterval2.1 Description let timerId = setInterval(func|code, delay, arg1, arg2, ...) 2.2 Parameters
2.3 Return Value The return value 2.4 UsageThis is an example of clicking a button and incrementing a number every second; <p id="showNum"></p> <button onclick="timer()">Click me to increase the number by one every second</button> <script> const showNum = document.getElementById("showNum"); let timerId; // Timer ID let num = 0; function timer() { timerId = setInterval(addNum, 1000); } function addNum() { showNum.innerText = `${num++}`; } // No code to stop the timer is written</script> 3. setTimeout3.1 Description let timerId = setTimeout(func|code, delay, arg1, arg2, ...) 3.2 Parameters The parameters of
3.3 Usage The usage of However, unlike <p id="showNum"></p> <button onclick="timer()">After clicking, wait for one second and the number increases by one</button> <script> const showNum = document.getElementById("showNum"); let timerId; let num = 0; addNum(); function timer() { timerId = setTimeout(addNum, 1000); } function addNum() { showNum.innerText = `${num++}`; } </script> 4. Cancel the timer The usage is very simple, with only one parameter, which is clearInterval(intervalID); clearTimeout(timeoutID);
Usage is simple: function timer() { timerId = setTimeout(addNum, 1000); } clearTimeout(timerId); // When the code runs to this line, the timer set by timer will be canceled. 5. Use timers in the consoleYou can also use timers in the browser console console.time(timerName) Create a timer named name and start it.
console.timeEnd(timerName) Calling console.time(timerName); console.timeEnd(timerName); 5.1 Usage For loop 99999 times how much time example: console.time(name); let num; for (let index = 0; index < 99999; index++) { num++; } console.timeEnd(name); This is the end of this article about the details of JavaScript timer. For more relevant JavaScript timer content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Why Google and Facebook don't use Docker
>>: Interpreting MySQL client and server protocols
CSS Viewport units have been around for the past ...
Recently, I have been studying the MySQL database...
background First of all, I would like to state th...
Table of contents 2. Comma operator 3. JavaScript...
Docker Toolbox is a solution for installing Docke...
Table of contents Preface: accomplish: Summarize:...
Click here to return to the 123WORDPRESS.COM HTML ...
For detailed documentation on installing the comp...
1. Unzip the zip package to the installation dire...
#Case: Query employee salary levels SELECT salary...
This article uses examples to describe the common...
The knowledge points summarized below are all fre...
Preface In the daily development or maintenance o...
The rewrite module is the ngx_http_rewrite_module...
Since the introduction of the contentEditable attr...