Differences and usage of setInterval and setTimeout in js

Source: Internet
Author: User

SetTimeout

SetTimeout () //-run the code after the specified time
ClearTimeout () //-cancel setTimeout ()


Note: setTimeout () and clearTimeout () are both functions of the Window object of html dom.


Usage: setTimeout () is used to call a function or computing expression after a specified number of milliseconds.
Syntax: setTimeout (code, millisec)
Code (required): the JavaScript code string to be executed after the function to be called.
Millisec (required): The number of milliseconds to wait before executing the code.
Note: setTimeout () is executed only once. If you want to call it multiple times, use setInterval () or let the code itself call setTimeout () again ().
 
Two Methods of calling a function:
Function page_list (){
Alert ("shihuan ");
}
Window. setTimeout (page_list, 5000); // It indicates that the page_list () function is executed in 5 seconds.
Window. setTimeout ("page_list ()", 30000); // indicates that the page_list () function is executed 30 seconds after the delay.
 
You can use clearTimeout to stop timing at any time.


Application Skills
We recommend that you set setTimeout as a function. For example:

Function delayRun (code, time ){
Var t = setTimeout (code, time );
}

In this way, when you need to delay the execution of a code segment, you only need to add this function before the code segment. For example:
Onmouseover = delayRun ("setTab (500)
SetTab is a custom function. If you do not want to delay the execution of setTab in the future, remove the delayRun code in the statement,
Change to onmouseover = setTab (0, 0.
This method avoids writing a piece of setTimeout code for every place that requires latency. You only need to call it directly, which is very convenient. It also saves the amount of code.

Bytes --------------------------------------------------------------------------------------------------
 
SetInterval


The setInterval () method can call functions or computation expressions according to the specified period (in milliseconds.


The setInterval () method does not stop calling the function until clearInterval () is called or the window is closed.
The ID value returned by setInterval () can be used as a parameter of the clearInterval () method.


Syntax

SetInterval (code, millisec)
Code required. The function to be called or the code string to be executed.
Millisec required. The interval between periodical execution or call codes, in milliseconds.

A value that can be passed to Window. clearInterval () to cancel periodic code execution.


Example:
Var leftSeconds = 10;
Var intervalId;
$ (Function (){
$ ("# BtnReg"). attr ("disabled", true );
IntervalId = setInterval ("countDown ()", 1000 );
});
Function countDown (){
If (leftSeconds <= 0 ){
$ ("# BtnReg"). val ("submit ");
$ ("# BtnReg"). attr ("disabled", false );
ClearInterval (intervalId );
Return;
} Else {
LeftSeconds --;
$ ("# BtnReg"). val ("please read carefully" + leftSeconds + "seconds ");
}
}


Author: angus_17

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.