SetTimeout () usage and precautions in Javascript

Source: Internet
Author: User

SetTimeout (code, millisec) method. If the code of the 1st parameter calls the function name directly, a global or local function call problem exists. Note the following when using the setTimeout (code, millisec) method:

SetTimeout (expression, interaction time)
The latency/interaction time is in the unit of haoss (1000 ms = 1 s). When setTimeout is executed, it is executed once after the specified delay time after loading, and only once

When setTimeout is executed, it executes the expression once every specified time after loading.

1. Basic usage:
 
Run a piece of code:

The Code is as follows: Copy code

Var I = 0;
SetTimeout ("I + = 1; alert (I)", 1000 );

Execute a function:

The Code is as follows: Copy code

Var I = 0;
SetTimeout (function () {I + = 1; alert (I) ;}, 1000 );

// Compare the two methods above.


SetTimeout ()

The Code is as follows: Copy code

<Script type = "text/javascript">
Function test (){
Alert ("I Am a global ");
}
Function myfun (){
Function test (){
Alert ("I am local ");
}
SetTimeout ("test ()", 2000); // call the global test
SetTimeout (test, 5000); // call the local test
}
Myfun ();
</Script>


The running result is that alert first outputs "I Am a global" and then alert outputs "I am a local "!

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.