异步JavaScript的返回值/分配使用jQuery [英] JavaScript asynchronous return value / assignment with jQuery

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

问题描述

我有以下的jQuery功能。我试图返回)在警报(这里显示的GUID值; 警报工作正常,并填充了值,但我似乎无法将其分配到一个变量,并返回其值。

最后,我需要其他功能来访问GUID值,等等一切我已经试过只显示为未定义

我想要做这样的事情:

 函数trackPage(){
    VAR elqTracker =新jQuery.elq(459);
    elqTracker.pageTrack({
        成功:函数(){
            elqTracker.getGUID(功能(GUID){
                警报(GUID);
                VAR的returnValue = GUID;
            });
        }
    });
    返回的returnValue;
}变种someGuid = trackPage();


解决方案

所以,这个问题已经被问过无数次,我敢肯定,每个人(包括我自己)的试过这个曾经的。

这仅仅是一个异步调用的本质,你不能用他们的结果为收益值。这就是为什么他们有你在得到调用的结果,他们不能收益,要么一个函数传递!还要注意, elqTracker.pageTrack()函数调用立即返回,因此你的的returnValue 简直就是未定义

大多数人(见dfsq的答案)通过引入一个回调函数作为paramater解决这个问题。这种方法试过了,真&ndash的;然而,jQuery有 $。递延 。这样您就可以制作自己的异步逻辑返回然后你就可以将任何数量的回调为:

 函数trackPage(){
    VAR elqTracker =新jQuery.elq(459)
        DFD = $ .Deferred();    elqTracker.pageTrack({
        成功:函数(){
            elqTracker.getGUID(功能(GUID){
                dfd.resolve(GUID);
            });
        }
    });    返回dfd.promise();
}//例如使用:
trackPage()。完成(功能(GUID){
    警报(得到GUID:+ GUID);
});

注意,现在你的 trackPage()返回一个对象,你可以将回调?你不必附上他们要么立即

  VAR pageHit = trackPage()。完成(功能(GUID){
    警报(页命中GUID:+ GUID);
});$(按钮)。点击(函数(){
    pageHit.done(功能(GUID){
        警报(点击了页面GUID:+ GUID);
    });
});

另外,jQuery的AJAX模块总是返回的承诺一样,所以如果你让你自己的逻辑回报承诺为所有的AJAX的东西接口应该是非常相似的。


作为一个方面说明:我想指出的是,你的 VAR的returnValue 是错误的范围反正。它需要在 trackPage 函数的外部范围中声明。即使有此修复程序,这个概念仍然不能正常工作。

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.

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

I'd like to do something like this:

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.

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.

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 );
});

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 );
    });
});

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.


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天全站免登陆