Home > Web Front-end > JS Tutorial > javascript setTimeout() passes function parameters (including passing object parameters)_javascript skills

javascript setTimeout() passes function parameters (including passing object parameters)_javascript skills

WBOY
Release: 2016-05-16 18:30:28
Original
1403 people have browsed it

So, I searched online and used another writing method setTimeout("fun(" parameter ")", 1000), but it still didn't work. However, the above writing method is OK when passing the textarea of ​​the form form. For example, hml is like this:

Copy code The code is as follows:















In js I can write like this:
setTimeout("doAjax(document. sform.txtara.value )", 1000);
But once you want to pass other parameters or object parameters, you have to rewrite this function according to the methods provided on the Internet.
After research and practice, I have improved the method myself. You can refer to it. I only base it on my personal needs. I cannot guarantee other situations. Please give me your advice!
Copy code The code is as follows:
















JavaScript:
Copy code 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; // Mainly this 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);//Also This line
}

In this way, I solved the problem of object parameter passing. Finally, I would like to say that if you have a better solution, please leave a comment. I am happy to cooperate with like-minded people. study!
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template