JS window.settimeout function

Source: Internet
Author: User

Itimerid = Window.settimeout (Vcode, Imilliseconds [, Slanguage])


Parameters

Vcode Required. Variant that specifies the function pointer or string This indicates the code to is executed when the specified interval h As elapsed.
Imilliseconds Required. Integer that specifies the number of milliseconds.
Slanguage Optional. String that specifies one of the following Values:jscript Language is JScript.
VBScript Language is VBScript.
JavaScript Language is JavaScript.

Return Value

Integer. Returns An identifier this cancels the evaluation with the Cleartimeout method.

==============================================================

The above content is excerpted from a JScript tutorial (CHM format, with an unknown source, said to the original author sorry)


The following content did not copy anyone, if there are similarities, it is not estimated that you copy my coincidence, hehe.

-------------------------------------------------------------------
settimeout (Alert ("3 seconds Passed"), 3000);//Call a function that allows constant arguments
-------------------------------------------------------------------
<script language= "Javascript" >
by Zuoyang

var x = 1;
var y = 2;
var z = 3;

var sum;

function Plus (A, B)
{
var z = 0;
var i = 0;
for (i = 0; i < arguments.length; i++)
{
z = = Arguments[i];
}
settimeout (function () {alert (z);}, 6000); settimeout invocation form with variable arguments
return z;
}

settimeout (function () {sum = Plus (x, y, z);}, 3000);//* In addition to being able to take variable parameters can also get the return value of the settimeout call form * *

</script>

The use of SetInterval () is the same as settimeout ():

Itimerid = Window.setinterval (Vcode, Imilliseconds [, Slanguage])
The difference is that settimeout () is a one-time action, and SetInterval () is executed once every imilliseconds vcode. (evaluates a expression each time a specified number of milliseconds has elapsed)


Method Two.

settimeout (expression, delay time)
settimeout (expression, interaction time)
Delay Time/Interaction time is in Hao seconds (1000ms=1s)

SetTimeout at execution time, after the specified time, after loading, to execute an expression, execute 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:
var i=0;
SetTimeout ("I+=1;alert (i)", 1000);
To perform a function:
var i=0;
settimeout (function () {I+=1;alert (i);},1000);

Note the difference between the two methods above.

Here's another executive function:
var i=0;
function Test () {
I+=1;
alert (i);
}
settimeout ("Test ()", 1000);
You can also do this:
SetTimeout (test,1000);

Summarize:
The prototype of settimeout is this:
Itimerid = Window.settimeout (Vcode, Imilliseconds [, Slanguage])

There are two forms of settimeout

SetTimeout (Code,interval)
SetTimeout (Func,interval,args)

Where code is a string
Func is a function.

Note that the meaning of "function" is an expression, not a statement.
Like you want to perform a function periodically
function A () {
//...
}
can be written as
SetTimeout ("A ()", 1000)
Or
SetTimeout (a,1000)

Note that in the second form, it is a, do not write a (), remember!!!
Unfold, no matter what you write here, if it is a variable, it must be a variable that points to a function, and if it is a function, its return value will be a function.

2, realize the function of setinterval with settimeout
The idea is very simple, that is, in a function called non-stop execution itself, a bit like recursion
var i=0;
function Xilou () {
I+=1;
if (i>10) {alert (i); return;}
SetTimeout ("Xilou ()", 1000);
You can use this, too.
SetTimeout (xilou,1000);
}

3, using settimeout in the class
Finally to the point, in fact, the use of the class in the problem is all about this, as long as the solution to this question on everything is no worries.
Oh. Let's analyze:

function Xilou () {
By the West building cold month www.chinacms.org
This.name= "Xilou";
this.sex= "Male";
this.num=0;
}
Xilou.prototype.count=function () {
This.num+=1;
alert (this.num);
if (this.num>10) {return;}
The following four methods of testing, one by one rotation test.
SetTimeout ("This.count ()", 1000)//a: An error occurs when the following X.count () Call: The object does not support this property or method.
SetTimeout ("Count ()", 1000);//b: Error display: Missing object
SetTimeout (count,1000);//c: Error display: ' Count ' not defined
Below is the fourth kind by the West building cold month www.111cn.net
var self=this;
settimeout (function () {self.count ();},1000);//d: Correct

}

var x=new xilou ();
X.count ();

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.