Code of Js setInterval and setTimeout (scheduled execution and cyclic execution) (parameters can be passed in)

Source: Internet
Author: User

Document methods:
Cyclic execution: var timeid = window. setInterval ("method name or method", "latency"); window. clearInterval (timeid );
Scheduled execution: var tmid = window. setTimeout ("method name or method", "latency"); window. clearTimeout (tmid );
Example:
A. When parameters are not required in the method to be executed
Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Execute cyclically, and execute showalert () every three seconds ()
Window. setInterval (showalert, 3000 );
Function showalert ()
{
Alert ("aaaaa ");
}
// Scheduled execution. Execute show () in 5 seconds ()
Window. setTimeout (show, 5000 );
Function show ()
{
Alert ("bbb ");
}
</Script>

B. When parameters are required in the method to be executed
Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Execute cyclically, and execute showalert () every three seconds ()
Window. setInterval (function (){
Showalert ("aaaaa ");
},3000 );
Function showalert (mess)
{
Alert (mess );
}
// Run the showalert () command in five seconds ()
Window. setTimeout (function (){
Showalert ("bbbbbb ");
},5000 );
</Script>

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.