JavascriptsetTimeout () transfer function parameters (including passing object parameters) _ javascript skills

Source: Internet
Author: User
As needed, I need to use setTimeout () and pass a parameter in the function parameter, just like setTimeout (& quot; fun (parameter) & quot;, 1000 ). However, in my writing, js will report an error saying that the 'parameter' is not defined. So I searched the internet and used another method, setTimeout ("fun (" + parameter + ")", 1000, however, the preceding method is applicable when passing form textarea. For example, hml is like this:

The Code is as follows:











In js, I can write like this:
SetTimeout ("doAjax(document.sform.txt ara. value)", 1000 );
However, if you want to pass other parameters or object parameters, You need to rewrite this function according to the method provided on the Internet.
After research and practice, I have improved my methods. You can refer to it for reference. I only rely on my personal needs. I cannot guarantee other situations. Please give me more advice!

The Code is as follows:












JavaScript:

The Code is as follows:


Function initAjax (){
Var httprequest = null;
Try {
Httprequest = new ActiveXObject ("Msxml2.XMLHTTP ");
}
Catch (e ){
Try {
Httprequest = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Catch (e ){
Try {
Httprequest = new XMLHttpRequest ();
}
Catch (e ){
Httprequest = null;
}
}
}
Return httprequest;
}
Function doAjax (msg, obj ){
Var obj = obj; // This line is the main line.
Alert (obj. value );
Var he = "he =" + msg;
Var ajaxrequest = initAjax ();
Ajaxrequest. open ("POST", "abc. jsp", true );
Ajaxrequest. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset = UTF-8 ");
Ajaxrequest. send (he );
Ajaxrequest. onreadystatechange = function (){
If (ajaxrequest. readyState = 4 ){
If (ajaxrequest. status = 200 ){
Document. getElementById ("showpane"). innerHTML = ajaxrequest. responseText;
}
Else {
DoAjax (msg );
}
}
}
SetTimeout ("doAjax(document.sform.txt ara. value, document. all [" + obj. sourceIndex + "])", 100); // there is also this line
}


In this way, I solved the problem of passing object parameters. Finally, I would like to say that I would like to comment on a better solution. I would like to learn from like-minded people!
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.