当前位置:网站首页>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 02——What‘s new in Chrome extensions
- Deep understanding Net (2) kernel mode 2 Kernel mode construct semaphone
- Wechat applet realizes map navigation + door-to-door recycling
- 我想问一下招商证券怎么开户?通过链接办理股票开户安全吗
- ot initialized – call ‘refresh’ before invoking lifecycle methods via the context: Root WebApplicati
- Go language mutex lock
- Chapter 13 signal (III) - example demonstration
- Geoffreyhinton: my 50 years of in-depth study and Research on mental skills
- Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
- ctfshow nodejs
猜你喜欢

Thinkphp5 log file contains trick

About the problems encountered when using the timer class to stop with a button (why does the QPushButton (for the first time) need to be clicked twice to respond?)

Zend studio how to import an existing project

Detailed explanation of the first three passes of upload Labs

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

Att & CK red team evaluation field (I)

Tencent two sides: @bean and @component are used on the same class. What happens?

go time. after

ctfshow nodejs

On simple code crawling Youdao translation_ 0's problem (to be solved)
随机推荐
PHP multidimensional array sorting
DefCamp Capture the Flag (D-CTF) 2021-22 web
PHP excel export function encapsulation (based on phpexcel class)
Use PHP to delete the specified text content in the file
Learn about data kinship JSON format design from sqlflow JSON format
Meaning of while (~scanf ("%d%d", & A, & B))
PHP reverses scenarios based on code and skillfully uses debug_ backtrace()
[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
Laravel configures passport and returns token using JWT
Small exercise of process and signal
I want to ask how to open an account at China Merchants Securities? Is it safe to open a stock account through the link
Why is the resolution of the image generated by PHP GD library 96? How to change it to 72
For loop and promise to solve the problem of concurrent callback
Go language func function
Solve the error in my QT_ thread_ global_ End(): 3 threads didn't exit
【 scientific literature measurement 】 mining and visualization of keywords in foreign and Chinese Literature
Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
More than 20 years after Hong Kong's return, Tupu digital twin Hong Kong Zhuhai Macao Bridge has shocked
Introduction to the renewal of substrate source code: the pallet alliance is incorporated into the main line,
想請教一下,我在佛山,到哪裏開戶比較好?手機開戶是安全麼?