当前位置:网站首页>Detailed explanation of settimeout() and setinterval()
Detailed explanation of settimeout() and setinterval()
2022-06-30 14:32:00 【Domineering ape General Manager】
JavaScript It's a single-threaded language , But it can specify that the code is executed at a specific time by setting the timeout value and the interval value . A time-out value is when code is executed after a specified time , The interval time value means that the code is executed every specified time .
Timeout call
The timeout call uses window Object's setTimeout() Method , It takes two arguments : The code to execute and the time in milliseconds ( Wait time before code execution ). among , The first argument can be a string ( and eval() Same as the string used in ), It can also be a function .

The second parameter is a number of milliseconds indicating how long to wait , But the code does not necessarily execute after that time .JavaScript It's an interpreter for one line programs , So only one piece of code can be executed in a certain period of time . To control the code to be executed , There is one JavaScript Task queue . These tasks are executed in the order they are added to the task queue .setTimeout() The second parameter of tells JavaScript How long will it take to add the current task to the queue . If the queue is empty , Then the added code will be executed immediately ; If the queue is not empty , The added code will be executed after the previous code is executed .
call setTimeout() after , This method will return a value ID, Indicates a timeout call . This timeout call ID Is the unique identifier of the planned execution code , It can be used to cancel the timeout call . Cancel timeout call using method clearTimeout();

Intermittent call
Intermittent calls are similar to timeout calls , It's just that it executes code repeatedly at specified intervals , Until the intermittent call is canceled or the page is unloaded . The way to set intermittent calls is setInterval(), The parameters it receives are the same as setTimeout() identical . Canceling an intermittent call is far more important than a timeout call .

But usually , Intermittent calls are rarely really used , Because the latter intermittent call may be called before the end of the previous intermittent call . therefore , We usually use timeout calls to simulate intermittent calls

Here are two small demo:
1、 Get the current date and let it appear in the text box , Click on “stop” The event after the button is still .( Intermittent call )
HTML Code :
<input type="text" size="50" id="clock" />
<input type="button" value="Stop" id="btn" />
JavaScript Code :
function clock(){
var time = new Date();
document.getElementById("clock").value = time;
var btn =document.getElementById("btn");
btn.onclick = function(){
clearInterval(t);
}
}
var t = setInterval(clock,1000);
2、 Use setTimeou() To achieve the effect of counting and statistics , And display the value in the text box .
HTML Code :
<input type="text" id="count" />
JavaScript Code :
var num = 0;
function startCount(){
document.getElementById("count").value = num;
num += 1;
setTimeout(startCount,1000); //setTimeout It's a timeout call , Use recursion to simulate intermittent calls
}
setTimeout(startCount,1000); //1s After execution
( This question was once my interview topic , After being cheated by it, I went through various Baidu checks , I think it's not bad , So share it with you .)
边栏推荐
- "Persistent diseases" that cannot be solved in IM application development
- Numpy creates an empty array data = np empty(shape=[1, 64,64,3])
- JS delete the objects in the array and specify to delete the objects
- Notes on reverse learning in the first week of winter vacation
- Google Earth engine (GEE) - ghsl: global human settlements layer, built grid 1975-1990-2000-2015 (p2016) data set
- An error is reported when installing dataspherestudio doc: invalid default value for 'update_ time‘
- [buuctf] [geek challenge 2019] secret file
- Laravel RBAC laravel permission use
- Advanced usage of go language for loop break and continue
- Wechat applet realizes map navigation + door-to-door recycling
猜你喜欢

Getting started with shell Basics

Laravel upload error

QQ was stolen? The reason is

XSS challenge (6-10) more detailed answers

Introduction to the construction and development of composer private warehouse

深入理解.Net中的线程同步之构造模式(二)内核模式4.内核模式构造物的总结

"Persistent diseases" that cannot be solved in IM application development

DiceCTF - knock-knock

Thinkphp5 log file contains trick

I love network security for new recruitment assessment
随机推荐
LeetCode_ Stack_ Medium_ 227. basic calculator II (without brackets)
【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
DiceCTF - knock-knock
The JSON data returned from the control layer to JS has a "\" translator. How to remove it
Why is the resolution of the image generated by PHP GD library 96? How to change it to 72
Shell programming overview
从控制层返回到js的json数据带“\”转译符,怎么去掉
Laravel RBAC laravel permission use
數據恢複軟件EasyRecovery15下載
Configuration of headquarters dual computer hot standby and branch infrastructure for firewall Foundation
想请教一下,我在佛山,到哪里开户比较好?手机开户是安全么?
Intelligent operation and maintenance: visual management system based on BIM Technology
PHP multidimensional array sorting
【BUUCTF】 EasySql
Small exercise of process and signal
【Kubernetes系列】K8s设置MySQL8大小写不敏感
Numpy creates an empty array data = np empty(shape=[1, 64,64,3])
Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
Thinkphp5 log file contains trick
The programming competition is coming! B station surrounding, senior members and other good gifts to you!