简体   繁体   English

变量在函数更新值之前获取输出

[英]variable getting output before it's value is updated by a function

I have the following code that outputs the initialized value of my variable before it is updated by an $.ajax() callback. 我有以下代码,用于在$ .ajax()回调对其进行更新之前输出变量的初始化值。 It seems that the 'get' is performed after the variable is logged despite how the code is written. 尽管代码是如何编写的,但似乎在记录变量之后执行了“ get”操作。 How can I correct this? 我该如何纠正? Thanks for any suggestions. 感谢您的任何建议。

var statusUpdate = {
    queries : [],
    numQueries : 0,
    getNumQueries : function(){
        $.ajax({
            type: 'get',
            url: '40985839503175/planXML.txt',
            cache: false,
            error: function()
            { 

            },
            success: function(data){
                statusUpdate.queries = $(data).find('query');
                statusUpdate.numQueries = statusUpdate.queries.length;
                //console.log(statusUpdate.numQueries);

            }

        });

        }  // end getNumQueries

};


statusUpdate.getNumQueries();
console.log(statusUpdate.numQueries)
var statusUpdate = {
    queries : [],
    numQueries : 0,
    getNumQueries : function(){
        $.ajax({
            type: 'get',
            url: '40985839503175/planXML.txt',
            cache: false,
            error: function()
            { 

            },
            success: function(data){
                statusUpdate.queries = $(data).find('query');
                statusUpdate.numQueries = statusUpdate.queries.length;
                console.log(statusUpdate.numQueries);

            }

        });

        }  // end getNumQueries

};

Uncomment the logger in the success callback. 在成功回调中取消对记录器的注释。 Otherwise the log may run before the ajax call gets a chance to complete. 否则,日志可能会在ajax调用有机会完成之前运行。

Ajax calls are asynchronous. Ajax调用是异步的。 I'm guessing that you are seeing 0 in your console? 我猜您在控制台中看到0

Put the console.log inside the success function. console.log放入成功函数中。 But I'd recommend passing a callback or returning the promise object (it will require you to restructure your JavaScript). 但是我建议传递一个回调或返回promise对象(这将要求您重组JavaScript)。

var statusUpdate = {
    queries : [],
    numQueries : 0,
    getNumQueries : function(){
        $.ajax({
            type: 'get',
            url: '40985839503175/planXML.txt',
            cache: false,
            error: function()
            {

            },
            success: function(data){
                statusUpdate.queries = $(data).find('query');
                statusUpdate.numQueries = statusUpdate.queries.length;

                console.log(statusUpdate.numQueries)
            }

        });

        }  // end getNumQueries

};


statusUpdate.getNumQueries();

UPDATE : Here's what I meant by using a promise. 更新 :这就是我使用诺言的意思。 For more info, see Deferred Object and the jQuery.ajax() documentation 有关更多信息,请参见Deferred ObjectjQuery.ajax()文档。

var statusUpdate = {
    queries : [],
    numQueries : 0,
    getNumQueries : function(){
        var deferred = new $.Deferred();

        $.ajax({
            type: 'get',
            url: '40985839503175/planXML.txt',
            cache: false
        }).done(function(data){
                statusUpdate.queries = $(data).find('query');
                statusUpdate.numQueries = statusUpdate.queries.length;

                deferred.resolve();
        ).fail(function () {
            deferred.reject();
        });

        return deferred.promise();
    }  // end getNumQueries

};


statusUpdate.getNumQueries().done(function () {
    console.log(statusUpdate.numQueries)
});

Ok, I guess I can't make it work as envisioned. 好的,我想我无法按预期进行。 I added a complete function but it seems like kind of a workaround. 我添加了一个完整的功能,但这似乎是一种解决方法。 Is this a reasonable practice? 这是合理的做法吗? Thanks. 谢谢。

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

相关问题 为什么变量的值在 output 之前没有按预期更新? - Why is a variable's value not updated as expected before being output? 从函数jquery获取更新的变量值 - getting updated variable value from function jquery 从点击/更改功能内部获取更新的变量值 - Getting an updated variable value from inside a click/change function 未在 function onReady 中获取全局变量的更新值 - Not getting updated value of global variable inside function onReady 从函数返回值之前保存的空变量 - empty variable getting saved before value is returned from a function 如果我有一个分配给函数调用值的变量,如果函数调用的参数发生更改,是否可以更新该变量? - If I have a variable, assigned to the value of a function call, can that variable be updated if the function call's parameters are changed? 为什么当我调用函数“StopTimer”函数时我的变量“intervalId”没有更新值? - Why my variable "intervalId" is not getting the value updated when I call the function "StopTimer" function? 全局变量未从函数中更新 - Global variable is not getting updated from within a function 赋值后变量值未更新 - Value of variable not getting updated after assignment then(函数)没有在angularjs中用返回的promise值更新 - then(function) not getting updated with returned promise value in angularjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM