当前位置:网站首页>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(^_^)∠※!!!

边栏推荐
- App access kakaotalk three party login
- 时间复杂度、空间复杂度的学习总结
- Canal real-time parsing MySQL binlog data practice
- Cloud native application comprehensive exercise
- C language bracket matching (stack bracket matching C language)
- 【ManageEngine】局域网监控软件是什么,有什么作用
- Nacos installation guide on win system
- Google play APK uploads other international app stores
- Formal parameters, arguments, main function parameters, arrays or pointers as function parameters of the knowledge in every corner of C language
- New 1688 API access instructions
猜你喜欢

Docker compose install MySQL

AlphaFold揭示了蛋白质结构宇宙-从近100万个结构扩展到超过2亿个结构
![[leetcode sliding window problem]](/img/84/566d3805e52c358603694cdec69a13.png)
[leetcode sliding window problem]

T-sne降维

跨模态对齐 20220728

云原生应用综合练习上

Read the recent trends of okaleido tiger and tap the value and potential behind it

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

Canal real-time parsing MySQL binlog data practice

Nacos installation guide on win system
随机推荐
Comprehensive upgrade, all you can imagine is here -- JD API interface
Redis installation, cluster deployment and common tuning
New 1688 API access instructions
A ten thousand word blog post takes you into the pit. Reptiles are a dead end [ten thousand word pictures]
[MySQL] historical cumulative de duplication of multiple indicators
(update 20211130) about the download and installation of Jupiter notebook and its own configuration and theme
C语言300行代码实现扫雷(可展开+可标记+可更改困难级别)
Django uses pymysql module to connect mysql database
【HCIP】MPLS 基础
[leetcode sliding window problem]
T-sne降维
Use of resttemplate and Eureka
SQL question brushing: find the last of all employees_ Name and first_ Name and corresponding department number Dept_ no
【搜索】—— 迭代加深/双向DFS/IDA*
北京护照西班牙语翻译推荐
【HCIP】重发布及路由策略的实验
了解网址url的组成后 运用url模块、querystring模块和mime模块完善静态网站
HCIA配置实例(eNSP)
Groundwater, soil, geology and environment
一篇万字博文带你入坑爬虫这条不归路 【万字图文】