JavaScript settimeout () passing function parameters (including passing object parameters) _javascript Tips

Source: Internet
Author: User
Tags javascript settimeout
So, the Internet to find a bit, with another way of writing settimeout ("fun (" + parameter +) ", 1000), or not, but the above wording in the Transfer form form of the textarea is possible, such as HML is this:
Copy Code code 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.txtara.value); />
</div>
</form>
</div>
</div>
</body>

in JS I can write like this:
SetTimeout ("Doajax (Document.sform.txtara.value)", 1000);
However, once you pass the other parameters or object parameters, the method provided online is to rewrite the function.
After research and practice, I have improved the method, we can refer to the reference, I just according to my personal needs, other circumstances I can not guarantee, please advise!
Copy Code code 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.txtara.value, this); />
</div>
</form>
</div>
</div>
</body>

JavaScript:
Copy Code code 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 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.txtara.value,document.all[" +obj.sourceindex+ "])", 100;//There's a line.
}

In this way, I solved the problem of object parameter transmission, and finally want to say, can have a better solution please make comments, I am willing to learn with 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.