settimeout () Usage and attention problems in JavaScript

Source: Internet
Author: User

settimeout (expression, interaction time)
The delay time/interaction time is in Hao seconds (1000ms=1s) settimeout at execution time, after a specified time delay after loading, execute an expression once

SetTimeout at execution time, it executes an expression at a specified time, after it has been loaded

1, Basic usage:

Execute a piece of code:

The code is as follows Copy Code

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

To perform a function:

The code is as follows Copy Code

var i=0;
settimeout (function () {I+=1;alert (i);},1000);

Note the difference between the two methods above.


SetTimeout () issues to be noted

The code is as follows Copy Code

<script type= "Text/javascript" >
function Test () {
Alert ("I am global");
}
function Myfun () {
function Test () {
Alert ("I am Partial");
}
settimeout ("Test ()", 2000); To invoke global test
settimeout (test, 5000); To call local test
}
Myfun ();
</script>


The result is the first alert out of "I am global" and then alert out "I am part of"!

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.