JavaScript settimeout functions can be parameter/pass values

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. General direct reference, can only pass the parameters such as Word Fu, but for the object type can not be directly passed on, my method is to rewrite the Window.settimeout method, using the Apply back off,

The code is as follows Copy Code

var _st = window.settimeout;
Fref is the test function, 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 execute, the second argument is the interval, and the subsequent argument is the value to pass in
Window.settimeout (Test,1000,document.getelementbyid ("D"), ' abc ', ' 123 ');

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.