当前位置:网站首页>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 )
边栏推荐
- Alipay certificate mode payment interface
- Initial attack and defense world Misc
- Intelligent operation and maintenance: visual management system based on BIM Technology
- Introduction to reverse commissioning - VA and RVA conversion in PE 04/07
- Mutex lock, read / write lock, spin lock, pessimistic lock, and optimistic lock
- Deep understanding Net (2) kernel mode 3 Kernel mode construct mutex
- PHP conditional operator
- Experiment 2: stack
- 【BUUCTF】[GXYCTF2019]Ping Ping Ping1
- V3 02——What‘s new in Chrome extensions
猜你喜欢

Introduction to reverse commissioning - VA and RVA conversion in PE 04/07

Lifting scanning tool

Using docker to manage MySQL services under Windows

MySQL back to table query optimization
![[observation] as the intelligent industry accelerates, why should AI computing power take the lead?](/img/61/b446a616e86247507c27390505dc6b.jpg)
[observation] as the intelligent industry accelerates, why should AI computing power take the lead?

Learn about data kinship JSON format design from sqlflow JSON format

Google Earth engine (GEE) - ghsl: global human settlements layer, built grid 1975-1990-2000-2015 (p2016) data set

The programming competition is coming! B station surrounding, senior members and other good gifts to you!

Ctfshow getting started with the web (ThinkPHP topic)

The first dark spring cup dnuictf
随机推荐
I want to ask how to open an account at China Merchants Securities? Is it safe to open a stock account through the link
Laravel RBAC laravel permission use
NoViableAltException([email protected][])
org. json. The jsonobject object is converted to JSON, and JSON adds new elements. The value is obtained according to the JSON key. And list object format string to jsonarray
Upgrade composer self update
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
Laravel configures passport and returns token using JWT
Introduction to the renewal of substrate source code: the pallet alliance is incorporated into the main line,
Realize a simple LAN communication (similar to feiqiu)
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?)
Experiment 2: stack
Crypto questions
Deep understanding Net (2) kernel mode 3 Kernel mode construct mutex
从控制层返回到js的json数据带“\”转译符,怎么去掉
Zend studio how to import an existing project
1 figure to explain the difference and connection between nodejs and JS
Alipay certificate mode payment interface
Small exercise of process and signal
Comprehensively analyze the basic features and summary of free and paid SSH tools
Tencent two sides: @bean and @component are used on the same class. What happens?