当前位置:网站首页>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 )
边栏推荐
- V3_ Chrome extended Chinese translation document V3 directory
- 【BUUCTF】[GXYCTF2019]Ping Ping Ping1
- Not satisfied with markdown native code block style? Try this beautify code screenshot tool~~
- [scientific research data processing] [basic] category variable frequency analysis chart, numerical variable distribution chart and normality test (including lognormal)
- Introduction to reverse commissioning - VA and RVA conversion in PE 04/07
- Thoughts on the security of a PHP file name regular verification
- Go common lock mutex and rwmutex
- Data recovery software easyrecovery15 Download
- DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703
- 重磅:国产IDE发布,由阿里研发,完全开源!
猜你喜欢
MySQL back to table query optimization
Go language mutex lock
Why does the folder appear open in another program
Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source!
ThinkPHP v3.2 comment annotation injection write shell
Ctfshow getting started with the web (ThinkPHP topic)
Not satisfied with markdown native code block style? Try this beautify code screenshot tool~~
Apache Doris comparison optimization Encyclopedia
ctfshow nodejs
【 scientific literature measurement 】 mining and visualization of keywords in foreign and Chinese Literature
随机推荐
PHP conditional operator
【Kubernetes系列】K8s设置MySQL8大小写不敏感
数据恢复软件EasyRecovery15下载
【BUUCTF】 EasySql
NoViableAltException([email protected][])
Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
Shell programming overview
ctfshow nodejs
Configuration of headquarters dual computer hot standby and branch infrastructure for firewall Foundation
Getting started with shell Basics
Mysql database foundation: stored procedures and functions
深入理解.Net中的线程同步之构造模式(二)内核模式4.内核模式构造物的总结
Chapter 13 signal (III) - example demonstration
Laravel RBAC laravel permission use
PHP generate images into Base64
JS array
V3 03_ Getting started
What network security problems are exposed when a large-scale QQ number theft event occurs?
想請教一下,我在佛山,到哪裏開戶比較好?手機開戶是安全麼?
Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source!