JS SetTimeout FAQ Summary _ Basics

Source: Internet
Author: User
One, settimeout this point to the problem
This in settimeout ("This.count ()", 1000) refers to the Window object.
The settimeout of JS is defined as
Copy Code code as follows:

Window.settimeout=function (Vcode, Imilliseconds [, Slanguage]) {
//..... Code
Return timer//Returns a marker
}

So when you pass this to settimeout (), of course, it refers to the current object window to which it belongs.
Workaround:
1, before the call settimeout save this, such as self=this; SetTimeout ("Self.count ()", 1000);
2. Use jquery's $.proxy to change this point, such as $.proxy (SetTimeout ("This.count ()"), this);
ii. incoming parameters to settimeout
Copy Code code as follows:

function init () {
var url = "<%=basepath%>fetchwater.do?method=searchrealwater&xzqh=" + "<%=xzqh%>" + "&rand=" + Math.random ();
alert (URL);
Window.settimeout (function () {searchjdwater (URL);},100);
}

The test can be passed in any parameter, either string or other type, but be careful to use the above workaround when passing in this.
Attached is a more detailed link to the SetTimeout reference method http://www.jb51.net/article/40524.htm

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.