Application Examples of the js setTimeout Function

Source: Internet
Author: User

Application Examples of the js setTimeout Function

The window. setTimeout method is used to delay the execution of a function (method. However, it is defective when calling the specified method and passing parameters.

Infinite Loop

The Code is as follows:  

<Div id = "num"> </div>

<Script type = "text/javascript">
Window. onload = function ()
{
Var a = 0;
Function addNumber ()
{
A = a + 1;
Document. getElementById ("num"). innerHTML =;
Settime out (addNumber, 1000 );
}
AddNumber ();
}
</Script>

The setTimeout function can pass parameters/values.

The Code is as follows:  
Var _ st = window. setTimeout;
// FRef is the test function, and mDelay is the time
Window. setTimeout = function (fRef, mDelay ){
If (typeof fRef = 'function '){
Var argu = Array. prototype. slice. call (arguments, 2 );
Var f = (
Function (){
FRef. apply (null, argu );
});
Return _ st (f, mDelay );
}
Return _ st (fRef, mDelay );
}

Function test (x, y, z ){
Alert (x + "" + y + "" + z );
}

The first parameter is the function to be executed, the second parameter is the interval, and the following parameter is the value to be passed in.

The Code is as follows:  

Window. setTimeout (test, 1000, document. getElementById ("d"), 'abc', '123 ');

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.