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
The default firewall of CentOS7 is not iptables, ...
Table of contents 1. Installation 2. Introducing ...
1. Enter the Docker official website First, go to...
CentOS 6 and earlier versions provide MySQL serve...
Introduction MySQL should be a very common databa...
How to get the container startup command The cont...
Installation environment: CentOS7 64-bit MINI ver...
system: VMTOOLs Download: Link: https://pan.baidu...
This article shares with you the graphic tutorial...
Which parameter does the rpm command use to insta...
When using Zabbix custom scripts to collect monit...
Table of contents 1. Introduction 2. RC and RR is...
This article is welcome to be shared and aggregat...
In the process of writing the project page, I enc...
Tomcat7.0 sets virtual directory (1) Currently, o...