Brief Introduction In JavaScript, there are two timers: setInterval() and setTimeout(), each of which has a method to cancel the timer. These are all window objects, and window can be omitted when calling. These two methods are not in the JavaScript specification. There are four timer method related methods.
The difference between Note: setTimeout() is executed only once, while setInterval() is executed periodically at a given interval. setIntervaldescribe If There are multiple parameters for setInterval. First, if the first parameter is a code segment, then the setInterval() method is optional. Second, if the first parameter is a function, the setInterval() method can have multiple parameters. let timerId = setInterval(func|code, delay, arg1, arg2, ...) parameter
The parameter func|code usually passes in functions. For historical reasons, passing in a code string is supported, but is not recommended. Return ValueThe return value timeoutID is a positive integer, indicating the number of the timer. This value can be passed to clearTimeout() to cancel the timer. 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> setTimeoutdescribe let timerId = setTimeout(func|code, delay, arg1, arg2, ...) parameter The parameters of
The parameter usage: The usage of <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> Cancel timer The clearInterval() method cancels the timer set by setInterval(). The clearTimeout() method cancels the timer set by setTimeout(). The usage is very simple, with only one parameter, which is timeoutID, the identifier of the timer you want to cancel. clearInterval(intervalID); clearTimeout(timeoutID); Note that 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. Using the timer in the consoleYou can also use timers in the browser console console.time(timerName)Create a timer named name and start it. Each timer must have a unique name, and a maximum of 10,000 timers can run simultaneously on a page. console.timeEnd(timerName)Call console.timeEnd(name) to stop the timer and print the elapsed time in milliseconds. console.time(timerName); console.timeEnd(timerName); usageExample of how long it takes for a for loop to repeat 99999 times. console.time(name); let num; for (let index = 0; index < 99999; index++) { num++; } console.timeEnd(name); SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Several methods of implementing two fixed columns and one adaptive column in CSS
>>: Several reasons for not compressing HTML
Because the Base images pulled by Docker, such as...
We all know that Jmeter provides native result vi...
Let’s take the translation program as an example....
Table of contents Preface 1. Technical Principle ...
introduction As computing needs continue to grow,...
Preface: One day, I built a MySQL service in Dock...
Table of contents Add code to the Tree item; 1. S...
Table of contents 1. Environmental Preparation 2....
I'll record my first attempt at vue3.0. When ...
Table of contents 1 Test Environment 1.1 Server H...
Cocos Creator modular script Cocos Creator allows...
This article describes the Linux file management ...
Discuz! Forum has many configuration options in th...
<br />Table is a tag that has been used by e...
Whether MySQL needs to commit when performing ope...