简体   繁体   English

从AJAX函数内部修改外部变量?

[英]Modify outside variable from within AJAX function?

I use a AJAX request to get a numerical value from the database. 我使用AJAX请求从数据库中获取数值。 The AJAX function is inside another function that should return the value of the AJAX request. AJAX函数位于另一个应返回AJAX请求值的函数内部。 However because the return value from the AJAX request is a local variable inside the xmlhttp.onreadystatechange function it doesn't change the "higher level" temp_return of the return_count function. 但是,由于AJAX请求的返回值是xmlhttp.onreadystatechange函数内部的局部变量,因此它不会更改return_count函数的“更高级别” temp_return。 I can't have the "lower" function return the value and assign it to a variable because it's already defined to xmlhttp.onreadystatechange... How can I change this so that the return_count function will return the correct value instead of 42 (predefined for testing purposes)? 我不能让“低级”函数返回值并将其分配给变量,因为它已经定义为xmlhttp.onreadystatechange ...如何更改此值,以便return_count函数将返回正确的值而不是42(预定义)出于测试目的)?

function return_count(ajax_userid,ajax_date,ajax_KT,ajax_KS)
{
    var temp_return = 42;
    xmlhttp.onreadystatechange =
    function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            temp_return = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "count.php?userid="+ajax_userid+"&date="+ajax_date+"&KT="+ajax_KT+"&KS="+ajax_KS, true);
    xmlhttp.send();
    return temp_return;
}

You can do it of 2 ways... 您可以通过2种方式完成此操作...

making a callback to you ajax (RECOMENDED) 向您回调ajax(推荐)

creating async callback to continue your flow ;D 创建异步回调以继续您的流程; D

function return_count(ajax_userid,ajax_date,ajax_KT,ajax_KS, callback)
{   
    var otherCallback;
    var temp_return = 42;
    xmlhttp.onreadystatechange =
    function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            temp_return = xmlhttp.responseText;
            if(typeof callback === "function" ) callback(temp_return);
            if(typeof otherCallback === "function" ) otherCallback(temp_return);
        }
    }
    xmlhttp.open("GET", "count.php?userid="+ajax_userid+"&date="+ajax_date+"&KT="+ajax_KT+"&KS="+ajax_KS, true);
    xmlhttp.send();

    return { done: function (callback2){
          otherCallback = callback2;

    } };
}

you can use this way like below... 您可以像下面这样使用...

parameter callback 参数回调

return_count( userid, date, KT, KS, function (count) {

     alert("returned " + count);

}); });

or pipe callback 或管道回调

    return_count( userid, date, KT, KS)
    .done( function (count) {

         alert("returned " + count);

     });

making a synchronous ajax 制作同步ajax

adding the "false" to flag async... 添加“ false”以标记异步...

function return_count(ajax_userid,ajax_date,ajax_KT,ajax_KS)
{
    var temp_return = 42;
    xmlhttp.onreadystatechange =
    function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            temp_return = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "count.php?userid="+ajax_userid+"&date="+ajax_date+"&KT="+ajax_KT+"&KS="+ajax_KS, false);
    xmlhttp.send();
    return temp_return;
}

but this method lock your UI and is bad to UX. 但是这种方法会锁定您的用户界面,对UX不利。

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

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