JavaScript 异步返回值/jQuery 赋值 [英] JavaScript asynchronous return value / assignment with jQuery

查看:53
本文介绍了JavaScript 异步返回值/jQuery 赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 jQuery 函数.我正在尝试返回 alert(); 此处显示的 GUID 值警报工作正常并且值已填充,但是我似乎无法将其分配给变量并返回其值.

I have the following jQuery Function. I'm trying to return the GUID value shown here in the alert(); The alert works fine and the value is populated, however I can't seem to assign it to a variable and return its value.

最终我需要在其他函数等中访问 GUID 值.我尝试过的所有内容都只显示为 undefined.

Ultimately I need to access the GUID value in other functions, etc. Everything I've tried only displays as undefined.

我想做这样的事情:

function trackPage(){
    var elqTracker = new jQuery.elq(459);
    elqTracker.pageTrack({
        success: function() {
            elqTracker.getGUID(function(guid) {
                alert(guid);
                var returnValue = guid;
            });
        }
    });
    return returnValue;
}

var someGuid = trackPage();

推荐答案

所以,这个问题已经被问了一百万次了,我相信每个人(包括我自己)试过一次.

So, this question has been asked a million times over, and I'm sure that everyone (myself included) tried this once.

这只是异步调用的性质,您不能将它们的结果用作 return 值.这就是为什么他们让你传入一个获取调用结果的函数,他们也不能return!还要注意 elqTracker.pageTrack() 函数调用立即返回,因此您的 returnValue 只是 undefined.

It is just the nature of an asynchronous call, you can't use their results as a return value. Thats why they have you passing in a function that gets the result of the call, they can't return it either! Also notice that the elqTracker.pageTrack() function call returns IMMEDIATELY, therefore your returnValue is simply undefined.

大多数人(参见 dfsq 的回答)通过引入回调函数作为参数来解决这个问题.这个方法试过了,确实有效但是 jQuery 有 $.Deferred.这允许您让自己的异步逻辑返回一个 promise,然后您可以将任意数量的回调附加到:

Most people (see dfsq's answer) solve this problem by introducing a callback function as a paramater. This method is tried, and true – however jQuery has $.Deferred. This allows you to make your own asynchronous logic return a promise which you can then attach any number of callbacks to:

function trackPage(){
    var elqTracker = new jQuery.elq( 459 ),
        dfd = $.Deferred();

    elqTracker.pageTrack({
        success: function() {
            elqTracker.getGUID(function( guid ) {
                dfd.resolve( guid );
            });
        }
    });

    return dfd.promise();
}

// example use:
trackPage().done(function( guid ) {
    alert( "Got GUID:" + guid );
});

注意到您的 trackPage() 返回了一个可以附加回调的对象吗?您也不必立即附加它们.

Notice now that your trackPage() returns an object that you can attach callbacks to? You don't have to attach them immediately either.

var pageHit = trackPage().done(function( guid ) {
    alert( "Page Hit GUID:" +guid );
});

$("button").click(function() {
    pageHit.done( function( guid ) {
        alert( "Clicked on Page GUID:" + guid );
    });
});

此外,jQuery AJAX 模块也总是返回承诺,因此如果您使自己的逻辑返回承诺,则所有 AJAX 内容的界面应该非常相似.

Also, the jQuery AJAX module always returns promises as well, so the interface for all your AJAX stuff should be very similar if you make your own logic return promises.

作为旁注:我想指出您的 var returnValue 无论如何都在错误的范围"中.它需要在 trackPage 函数的外部范围内声明.即使有了这个修复,这个概念仍然不起作用.

As a side note: I'd like to point out that your var returnValue was in the wrong "scope" anyway. It needed to be declared in the outer scope of the trackPage function. Even with this fix, the concept still doesn't work.

这篇关于JavaScript 异步返回值/jQuery 赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆