Several application examples of JS settimeout function

Source: Internet
Author: User

The Window.settimeout method is used to delay the execution of a segment function (method). But it is in a certain defect when calling the specified method and passing the parameter.

Infinite loop

The code is as follows

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

<script type= "Text/javascript" >
Window.onload=function A ()
{
var a=0;
function Addnumber ()
{
a=a+1;
document.getElementById ("num"). Innerhtml=a;
SetTimeout (addnumber,1000);
}
Addnumber ();
}
</script>

The settimeout function can be used to pass a parameter/value

  code is as follows &nbs P;
var _st = window.settimeout;
//fref is the test function, Mdelay is time
Window.s Ettimeout = function (Fref, mdelay) { 
   if (typeof fref = = ' function ') {  
  & nbsp;    var Argu = Array.prototype.slice.call (arguments,2);  
        var f = (
            function () { 
& nbsp;               fRef.apply (null, ARGU) ;  
           });   
        return _st (f, mdelay);  
   } 
    return _st (Fref,mdelay);
}
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp
Function Test (x,y,z) { 
    alert (x+   +y+ "+z);
}

The first parameter is the function to execute, the second argument is the interval, and the subsequent argument is the value to pass in

The code is as follows

Window.settimeout (Test,1000,document.getelementbyid ("D"), ' abc ', ' 123 ');

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.