JavaScript settimeout and SetInterval Execute command function method

Source: Internet
Author: User
Tags setinterval javascript settimeout

Web page effects settimeout and SetInterval Execute command function method periodically

settimeout (expression, delay time)
At execution time, after the specified time is delayed, the expression is executed once, remembering that the number is once

Automatic changes with settimeout to display the effect of random numbers:

Window.settimeout ("Softphoneui.unregistergk ()", 10000000);

Or

Window.settimeout (softphoneui.unregistergk,10000000);

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:
Copy code code as follows:

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

To perform a function:
Copy code code as follows:

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

Note the difference between the two methods above.

Here's another executive function:
Copy code code as follows:
var i=0;
function Test () {
I+=1;
alert (i);
}
settimeout ("Test ()", 1000);

You can also do this:
Copy code code as follows:
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)


How to use SetInterval

[SetInterval]
SetInterval (expression, interaction time)
is not the same, it executes an expression once at a specified time from loading

Automatic changes with setinterval to display the effect of random numbers:

<script>
Function sett ()
{
Document.body.innerhtml=math.random ();
}
SetInterval ("Sett ();",
</script>
</script>
<body>
</body>

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.