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
Table of contents Preface iframe implements sandb...
Table of contents Preface How does antd encapsula...
This article describes how to compile and install...
Look at the code: Copy code The code is as follows...
Reference Documentation Official Docker installat...
Table of contents Preface System environment Curr...
Step 1: Add Ubuntu source Switch to root su root ...
1. Command Introduction The ln command is used to...
This article mainly records the problems and solu...
Table of contents 1. Set Deduplication 2. Double ...
01PARTCoreWebApi tutorial local demonstration env...
Table of contents Preface Detect Zookeeper servic...
When you first start using Docker, you will inevi...
When creating a MySQL container with Docker, some...
It seems that the mysql-sever file for installing...