Javascript setTimeout () transfer function parameters (including passing object parameters)

Source: Internet
Author: User
Tags javascript settimeout

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:
Copy codeThe Code is as follows:
<Body>
<Div class = "main">
<Div id = "showpane" class = "showpane"> </div>
<Div class = "textarea">
<Form method = "post" action = "" name = "sform">
<Textarea name = "txtara"> </textarea>
<Div>
<Input type = "submit" name = "subt" value = "send" onclick = "doAjax(sform.txt ara. value); return false;"/>
</Div>
</Form>
</Div>
</Div>
</Body>

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!
Copy codeThe Code is as follows:
<Body>
<Div class = "main">
<Div id = "showpane" class = "showpane">
</Div>
<Div class = "textarea">
<Form method = "post" action = "" name = "sform">
<Textarea name = "txtara"> </textarea>
<Div>
<Input type = "submit" name = "subt" value = "send" onclick = "doAjax(sform.txt ara. value, this); return false;"/>
</Div>
</Form>
</Div>
</Div>
</Body>

JavaScript:
Copy codeThe 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!

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.