当前位置:网站首页>The difference between settimeout() and setinterval()
The difference between settimeout() and setinterval()
2022-06-30 14:32:00 【Domineering ape General Manager】
summary
JavaScript Two types of timers are provided in setTimeout() And setInterval(), since JavaScript Two methods are provided , So what's the difference between the two methods ? Let's take a look today .
setTimeout() :
setTimeout() Only execute code once , See the following code if you don't understand :
function uw3cTimed()
{
var i = 1;
var t=setTimeout(function(){
document.write(i)
i++;
},1000)
}
uw3cTimed();
setInterval():
setInterval() Will execute N return , As long as you don't stop , It will always execute .
function uw3cTimed()
{
var i = 1;
var t=setInterval(function(){
document.write(i)
i++;
},1000)
}
uw3cTimed();
Although the above code is executed , You will see every... In the page 1 Seconds will add a number :1 2 3 4 5 6 7… Until you stop the timer or close the page .
How to use setTimeout() Realization setInterval() The effect of :
See the difference above , But I use setTimeout(), I want to achieve setInterval() What about the effect of ? Look at the code below :
var i = 1;
function uw3cTimed()
{
var t=setTimeout(function(){
document.write(i)
i++;
uw3cTimed();
},1000)
}
uw3cTimed();
How to stop the timer :
Now that you know the timer , So how do we stop or stop when certain conditions are met ?
function uw3cTimed()
{
var i = 1;
var t=setInterval(function(){
document.write(i)
if(i == 5){
clearTimeout(t)
}
i++;
},1000)
}
uw3cTimed();
See? , When i==5 Automatically stop when .
( Compared some explanations on Baidu , I put the difference between the two more easily understood , Share with you succinctly . from here )
边栏推荐
- [buuctf] [geek challenge 2019] secret file
- Pit used by go language array type
- Service online governance
- ThinkPHP v3.2 comment annotation injection write shell
- Three uses of golang underscores
- 【BUUCTF】 Have Fun
- C language & the difference between the address pointed to and the address pointed to by the pointer
- LeetCode_ Stack_ Medium_ 227. basic calculator II (without brackets)
- This editor will open source soon!
- Talk about Vue's two terminal diff algorithm, analysis of the rendering principle of the mobile terminal, and whether the database primary key must be self incremented? What scenarios do not suggest s
猜你喜欢

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

Google Earth engine (GEE) - ghsl: global human settlements layer, built grid 1975-1990-2000-2015 (p2016) data set
[email protected][])"/>NoViableAltException([email protected][])

Summary of use of laravel DCAT admin

【 scientific literature measurement 】 mining and visualization of keywords in foreign and Chinese Literature

2021 geek challenge Web

go channel && select

用Unity实现Flat Shading

DiceCTF - knock-knock

Go language mutex lock
随机推荐
Experiment 2: stack
Why does the folder appear open in another program
Solution cannot use a scalar value as an array
Pit used by go language array type
ctfshow nodejs
Fastcgi CGI shallow understanding
Problems in QT creator (additional unknown and error lines are listed in the debug output window)
Zend studio how to import an existing project
"As a service", the inevitable choice of enterprise digital transformation
Deep understanding Net (2) kernel mode 4 Summary of kernel pattern constructs
Initial attack and defense world Misc
V3 01_ Welcome
How to execute a query SQL
[scientific research data processing] [practice] frequency analysis chart of category variables, distribution chart of numerical variables and normality test (including lognormal)
When SQL queries are performed in table storage, an error is reported when the primary key is added to the query result, and the query result exceeds 10W rows. Do you want to add multiple indexes to t
Notes on reverse learning in the first week of winter vacation
The JSON data returned from the control layer to JS has a "\" translator. How to remove it
MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
remote: Support for password authentication was removed on August 13, 2021. Please use a personal ac
JS delete the objects in the array and specify to delete the objects