当前位置:网站首页>Timer of BOM series
Timer of BOM series
2022-07-29 01:34:00 【Old__ L】
window The object provides us with 2 It's a very easy way to use - Timer .
- setTimeout()
- setInterval()
1、setTimeout() Bomb timer
WindowOrWorkerGlobalScope Mixed setTimeout() Method to set a timer , The timer executes a function or a specified piece of code after the timer expires .
1.1、 Turn on timer
var timeoutID = scope.setTimeout(function[, delay, arg1, arg2, ...]);
var timeoutID = scope.setTimeout(function[, delay]);
var timeoutID = scope.setTimeout(code[, delay]);
1.1.1、 Parameters
- function
function It's when you want to expire (delay millisecond ) The function executed after .
- code
This is an optional Syntax , You can use strings instead of function , stay delay Compile and execute strings in milliseconds ( Using this syntax is not recommended , Reason and use eval() equally , There are security risks ).
- delay
Optional
The number of milliseconds delayed ( One second is equal to 1000 millisecond ), The call to the function will occur after this delay . If this parameter is omitted ,delay Take the default value 0, signify “ immediately ” perform , Or as soon as possible . In either case , The actual delay time may be longer than expected (delay Number of milliseconds ) Shift supervisor , Please check the reason why the actual delay is longer than the set value : Minimum delay time .
- arg1, …, argN
Optional
Additional parameters , Once the timer expires , They are passed as parameters to function
1.1.2、 Return value
Return value timeoutID Is a positive integer , Indicates the number of the timer . This value can be passed to clearTimeout() To cancel the timer .
1.1.3、 Example
<script> // The callback function is an anonymous function setTimeout(function () {
console.log(" time out "); }, 2000); function callback() {
console.log(" exploded "); } // The callback function is a well-known function var timer1 = setTimeout(callback, 3000); var timer2 = setTimeout(callback, 5000); </script>
1.2、 Stop timer clearTimeout()
WindowOrWorkerGlobalScope Built in clearTimeout() Method cancels the previous call setTimeout() Set up a timer .
1.2.1、 grammar
scope.clearTimeout(timeoutID)
1.2.2、 Parameters
- timeoutID
The identifier of the timer you want to cancel . The ID By the corresponding setTimeout() Call return .
1.2.3、 describe
Pass in a wrong ID to clearTimeout() There will be no impact ; It doesn't throw an exception .
1.2.4、 Example
<button> Click stop timer </button>
<script> var btn = document.querySelector("button"); // Turn on timer var timer = setTimeout(function () {
console.log(" exploded "); }, 5000); // Register the click event for the button btn.addEventListener("click", function () {
// Stop timer clearTimeout(timer); }); </script>
2、setInterval() Alarm timer
Window and Worker Interface provided setInterval() Method repeatedly calls a function or executes a code fragment , There is a fixed time interval between each call .
It returns a interval ID, The ID Uniquely identify the time interval , So you can call later clearInterval() To remove the timer .
2.1、 Turn on timer
var intervalID = setInterval(func, [delay, arg1, arg2, ...]);
var intervalID = setInterval(function[, delay]);
var intervalID = setInterval(code, [delay]);
2.1.1、 Parameters
- func
The function to be called repeatedly , Every time you specify delay Once in milliseconds . The first call occurred in delay In milliseconds .
- code
This syntax is optional , You can pass a string instead of a function object , The string you pass will be compiled and then passed every time delay Execute in milliseconds . This grammar is related to eval() There are the same safety risks, so it is not recommended to use .
- delay
Is the number of milliseconds per delay ( One second is equal to 1000 millisecond ), Each call to a function occurs after this delay . If not specified , The default value is 0. See the delay limit below for details delay Value range of .
- arg1, …, argN
Optional
When the timer expires , Will be passed on to func Additional parameters of the function .
2.1.2、 Return value
Return value intervalID Is a non-zero value , Used to mark passage setInterval() Timer created , This value can be used as clearInterval() To clear the corresponding timer .
2.1.3、 Example
<script> // 1. setInterval setInterval(function () {
console.log(" Continue to output "); }, 1000); </script>
2.2、 Stop timer clearInterval()
WindowOrWorkerGlobalScope mixin Of clearInterval() Method can cancel the previous pass setInterval() Set repeated scheduled tasks .
2.2.1、 grammar
scope.clearInterval(intervalID)
2.2.2、 Parameters
intervalID
Of the timer to cancel ID. By setInterval() Back to .
2.2.3、 Return value
undefined
Postscript
If you feel the article is not good
//(ㄒoㄒ)//, Just leave a message in the comments , The author continues to improve ;o_O???
If you think the article is a little useful , You can praise the author ;\\*^o^*//
If you want to progress with the author , Sure Wechat scan QR code , Focus on the front-end old L;~~~///(^v^)\\\~~~
Thank you readers(^_^)∠※!!!

边栏推荐
- C语言300行代码实现扫雷(可展开+可标记+可更改困难级别)
- 【搜索】—— 迭代加深/双向DFS/IDA*
- JS事件简介
- SQL question brushing: find the last of all employees who have been assigned departments_ Name and first_ Name and Dept_ no
- 2022年最火的十大测试工具,你掌握了几个
- C language 300 lines of code to achieve mine sweeping (deployable + markable + changeable difficulty level)
- 北京护照西班牙语翻译推荐
- 【HCIP】MPLS 基础
- els 方块移动
- Flask project construction 2
猜你喜欢

Use of resttemplate and Eureka

Flash reports an error: type object 'news' has no attribute' query 'the view name is duplicate with the model name

Flask reports an error: pymysq1.err OperationalError:(1054, “Unknown column ‘None‘ in ‘field list‘“)

Redis is installed on Linux

梅克尔工作室——HarmonyOS实现列表待办

Nacos installation guide on win system
![A ten thousand word blog post takes you into the pit. Reptiles are a dead end [ten thousand word pictures]](/img/aa/a5e7b4516aa395f8d4d0e2eee7d3c7.png)
A ten thousand word blog post takes you into the pit. Reptiles are a dead end [ten thousand word pictures]

SiC功率半导体产业高峰论坛成功举办

Openpyxl merge cells

After understanding the composition of the URL of the website, we use the URL module, querystring module and mime module to improve the static website
随机推荐
App access kakaotalk three party login
New 1688 API access instructions
Oozie工作调度
Django uses the existing data table method of MySQL database
Naver three party login
IT硬件故障的主要原因和预防的最佳实践
Regular checksum time formatting
Recommended Spanish translation of Beijing passport
PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益
Flink SQL Hudi actual combat
Comprehensive upgrade, all you can imagine is here -- JD API interface
Docker compose install MySQL
全新升级:获得淘宝商品详情“高级版” API
Canal实时解析mysql binlog数据实战
[MySQL] string to int
Flask reports an error: pymysq1.err OperationalError:(1054, “Unknown column ‘None‘ in ‘field list‘“)
Formal parameters, arguments, main function parameters, arrays or pointers as function parameters of the knowledge in every corner of C language
2022年最火的十大测试工具,你掌握了几个
Cookies and sessions
DocuWare 移动劳动力解决方案可帮助您构建新的生产力模式:随时随地、任何设备