在jQuery函数之外获取JSON响应变量? [英] Get JSON response var outside of jQuery function?

查看:71
本文介绍了在jQuery函数之外获取JSON响应变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下jQuery函数:

I have the following jQuery function:

function deleteHype(){
    FB.api('/me/launchhype:hype', function(response) {
            $.each(response.data, function(i, obj){
                    if (obj.data.launch.url == '<?php the_permalink(); ?>') {
                            var delete_launch_id = parentObj=obj.id;
                    }
            });
    });
}
alert(delete_launch_id);

让我们在IF语句中说var"delete_launch_id"为"X".好吧,如果我尝试在函数外执行警报"delete_launch_id",则它是未定义的".我希望它还给我"X"(假设的值).

Lets say in the IF statement the var "delete_launch_id" is "X". Well if I try to do the alert "delete_launch_id" outside the function it is "undefined". I want it to give me back "X" (the hypothetically value that was set).

我该怎么做?我迷路了,似乎找不到类似的例子.

How can I do this? I'm lost and can't seem to find a similar example.

推荐答案

根据流行的需求,下面将更深入地探讨为什么变量"delete_launch_id"在通过警报"功能访问时不可用

By popular demand, here's a more in depth look at why the variable "delete_launch_id" isn't available at the point where it's accessed by the "alert" function.

有几件事发生.

首先,FB.api函数很可能是异步AJAX调用. JavaScript并不是真正的异步(尚未),但是在这种情况下,它实际上是这样的.这意味着当异步代码执行时,异步代码等待响应时,它之后的代码将继续执行.因此,很有可能,如果您调用"deleteHype",然后紧随其后的警报",则在调用警报"时将不会返回FB.api响应.因此,该异步代码中设置的任何内容都将不可用.

First, the FB.api function is most likely an asynchronous AJAX call. JavaScript isn't truly asynchronous (yet), but in this case, it effectively behaves so. What that means is that when asynchronous code executes, code after it will continue executing while the async code waits for a response. So, there's a good chance that if you call "deleteHype", and then "alert" immediately following it, FB.api response won't have returned by the time "alert" is called. And as such, anything set within that async code won't be available yet.

第二个问题是JavaScript具有功能范围".实际上,这意味着在函数内部定义的变量仅在该函数内部可用.它在该功能内仅具有作用域".考虑以下代码:

The second issue is that JavaScript has "functional scope." What that means, in practice, is that a variable defined inside a function is only available inside that function. It only has "scope" within that function. Consider the following code:

var foo = 1;
if(1 === 1) {
    // non-function block. foo is accessible in here, and any changes to foo will be seen outside this block.
    foo = 2;
}
// foo is 2

function change() {
    // functional scope. variables defined in here are only available here
    foo = 3;
    var bar = 1;
}

// foo is 2 because we haven't called change yet
// bar is "undefined"

change();

// foo is 3
// bar is "undefined"

"foo"已更改,因为它在函数外部具有作用域,因此该函数可以访问它. 但是,栏"仅在该函数内部具有作用域,而在其他任何地方均不存在.

"foo" changed because it had scope outside the function, so the function can access it. "bar" however, only has scope inside that function and doesn't exist anywhere else.

在原始示例中,实际上在多个函数中声明了"delete_launch_id",因此在其他任何地方均不可用.

In the original example, "delete_launch_id" is declared inside several functions, actually, so it is not available anywhere else.

我的第一个答案是通过将一个函数作为回调"传递给"deleteType"函数来解决这些问题,该回调仅在异步代码完成并返回时才被调用.可以解决问题#1.然后,回调函数将"delete_launch_id"传递给它,从而使其可用.我给的格式似乎有点奇怪:

My first answer addresses these issues by passing a function into the "deleteType" function as a "callback" that will be called only when the async code is finished and returns. That handles issue #1. The callback function then has "delete_launch_id" passed to it, which makes it available. The format I gave may seem a little weird:

deleteHype(function(delete_launch_id) {
    // alert
});

这里发生的是实际上我们正在将函数作为变量传递.这是一个匿名"功能(无名称).所以deleteType就像变量一样(因为它是变量),并且可以调用它,因为它是一个函数,并且其中的代码将在调用时执行.这是一个非常方便的模式.

What's going on here is that we're actually passing a function as a variable. It's an "anonymous" function (no name). So deleteType treats is like a variable (because it is), and can call it, because it's a function, and the code inside it will execute when it's called. It's a really handy pattern.

我希望能有所帮助.一开始这很令人困惑.

I hope that helps a bit. This is confusing stuff at first.

这篇关于在jQuery函数之外获取JSON响应变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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