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
MySQL downloads for all platforms are available a...
Several commonly used string methods in JavaScrip...
Table of contents 1. First, configure the main.js...
introduction: There are a lot of information and ...
Preface: This article is based on the experience ...
①. How to use the alias (CNAME) record: In the do...
Run the command: glxinfo | grep rendering If the ...
1. HTML Image <img> 1. The <img> tag ...
The operating environment of this tutorial: Windo...
Table of contents Vue custom directive Custom dir...
By adding the current scroll offset to the attrib...
The Document Object Model (DOM) is a platform, a ...
One environment Install VMware Tools on CentOS 7 ...
Without further ado, I will post the code for you...
What is a table? It is composed of cell cells. In...