简体   繁体   English

通过参数传递时,AJAX setTimeout不起作用

[英]AJAX setTimeout not working when pass through parameters

The user inputs something then using ajax the program outputs something to the user. 用户输入一些内容,然后使用ajax程序向用户输出一些内容。 Which mostly works, the problem is passing variables through parameters to the remote function from my script. 在大多数情况下,问题是将变量通过参数从脚本传递到远程函数。

The setTimeout call messes up when I pass the text in as a parameter. 当我将文本作为参数传递时, setTimeout调用陷入混乱。 But when I type it in setTimeout function itself, it works: 但是当我在setTimeout函数本身中键入它时,它可以工作:

function handleServerResponse(text){
    if(xmlHttp.readyState==4){ 
        if(xmlHttp.status==200){
            xmlResponse = xmlHttp.responseXML; //izvlaci se xml sto smo dobili
            xmlDocumentElement = xmlResponse.documentElement;
            message = xmlDocumentElement.firstChild.data;
            document.getElementById('underInput').innerHTML = message;
            setTimeout("process(text)", 1000);
        }else{
            alert('Someting went wrong !');
        }
    }
}

The whole code: 整个代码:

<script type = "text/javascript">

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject(){
    var xmlHttp;

    if(window.ActiveXObject){ 
        try{
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(e){
            xmlHttp = false;
        }
    } else { 
        try{
            xmlHttp = new XMLHttpRequest();
        }catch(e){
            xmlHttp = false;
        }
    }

    if(!xmlHttp)
        alert("Cant create that object !")
    else
        return xmlHttp;
}

function process(text){

    //text = 'userInput';

    if(xmlHttp.readyState==0 || xmlHttp.readyState==4){
        food = encodeURIComponent(document.getElementById(text).value);
        xmlHttp.open("GET", 'foodstore.php'+"?food="+food,true);

        xmlHttp.onreadystatechange = function() {
            handleServerResponse(text);
        };

        xmlHttp.send(null);
    }else{
        setTimeout("process(text)",1000);//cekaj 1s pa probaj opet
    }
}

function handleServerResponse(text){

    if(xmlHttp.readyState==4){ 
        if(xmlHttp.status==200){
            xmlResponse = xmlHttp.responseXML; //izvlaci se xml sto smo dobili
            xmlDocumentElement = xmlResponse.documentElement;
            message = xmlDocumentElement.firstChild.data;
            document.getElementById('underInput').innerHTML = message;
            setTimeout("process(text)", 1000);
        }else{
            alert('Someting went wrong !');
        }
    }
}

</script>

//*******************************************

<html>
    <body onload = "process('userInput')">
        <h3>The Chuff Bucker</h3>
        Enter the food you would like to order:
        <input type="text" id="userInput" />
        <div id="underInput" />
    </body>
</html>

When you pass a string to setTimeout , it is evaluated in global scope. 将字符串传递给setTimeout ,将在全局范围内对其进行评估。 There is no global text variable, so the call will fail. 没有全局text变量,因此调用将失败。 Pass a function instead: 传递一个函数代替:

setTimeout(function() {
    process(text);
}, 1000);

Try to remove "" in 尝试在中删除“”

setTimeout("process(text)", 1000);

[EDITING FOR REFERENCE] instead like this one [参考编辑]就像这样

setTimeout(function(){
    process(text);
}, 1000);

Your AJAX query is needlessly complex. 您的AJAX查询不必要地复杂。 Are you able to use JQUERY at all? 您完全可以使用JQUERY吗? Its syntax is SUPER simple and its ease of implementation could potentially solve your issue. 它的语法超级简单,易于实施可能会解决您的问题。 Please take a look at this example. 请看这个例子。

AJAX SYNTAX FOR JQUERY 用于JQUERY的AJAX语法

var var_one = 'first post item here';
var var2 = $("#id_of_input_field").val(); 
///////// AJAX //////// AJAX //////////
    $.ajax({
        type: 'POST',
        url:  'file_to_receive_post_vars.php',
        data: {var_one:var_one, var2:var2},
        success: function( response ){
            alert('yay ajax!');
        }//close succss params
    });//close ajax
///////// AJAX //////// AJAX //////////

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM