javascript 无法在函数内访问 jQuery $.get 中的全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4614714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:17:55  来源:igfitidea点击:

Can't access global variable in jQuery $.get within function

javascriptjqueryglobal-variables

提问by Will

Below is some code I'm having trouble with. Basically, I'm defining an empty array as a global variable (var playlist = []) and then trying to add elements to it within a jQuery $.get call. From what I've read on the internet, I should be able to do this! The following code gives the error: "Cannot call method 'play' of undefined". playlist[0] does get set within the function, alerting playlist[0] within the $.get call gives the expected result, but it doesn't persist outside the function.

下面是一些我遇到问题的代码。基本上,我将一个空数组定义为全局变量 (var playlist = []),然后尝试在 jQuery $.get 调用中向其添加元素。从我在互联网上阅读的内容来看,我应该能够做到这一点!以下代码给出了错误:“无法调用未定义的方法‘播放’”。playlist[0] 确实在函数内设置,在 $.get 调用中提醒 playlist[0] 给出了预期的结果,但它不会在函数外持续存在。

var playlist = [];
function playArtist(artist){
  $.get('media/songs/' + artist,
    function(data){
      for (var i in data){
        playlist[i] = setSong(data[i].Resource.name,'track' + data[i].Media.id,i + 1);
      }
    $('#track-total').text(parseInt(playlist.length));
    },'json'
  );
  playlist[0].play();
}

Can anyone help?

任何人都可以帮忙吗?

Thanks!

谢谢!

采纳答案by karim79

Chances are, playlistis getting used before $.getreturns - as ajax calls are asynchronous. It works withinthe success callback because that gets fired once the request has completed, so it will contain the data you expect.

机会是,playlist$.get返回之前被使用- 因为 ajax 调用是异步的。它成功回调中工作,因为一旦请求完成就会触发它,因此它将包含您期望的数据。

回答by Dom

You don't have to do any of this. I ran into the same problem with my project. what you do is make a function call inside the on success callback to reset the global variable. As long as you got asynchronous javascript set to false it will work correctly. Here is my code. Hope it helps.

你不必做任何这些。我的项目遇到了同样的问题。您所做的是在成功回调中进行函数调用以重置全局变量。只要您将异步 javascript 设置为 false,它就会正常工作。这是我的代码。希望能帮助到你。

 var exists;

 //function to call inside ajax callback 
 function set_exists(x){
     exists = x;
 }

  $.ajax({
     url: "check_entity_name.php",
     type: "POST",
     async: false, // set to false so order of operations is correct
     data: {entity_name : entity},
     success: function(data){
     if(data == true){
        set_exists(true);
     }
     else{
        set_exists(false);
     }
  }
});

if(exists == true){
    return true; 
}
else{
    return false;
}

Hope this helps you .

希望这对你有帮助。

回答by Lazarus

.get is asynchronous, hence the need to provide a callback function. While your get is still in process you are trying to use the array, probably before it's actually been populated.

.get 是异步的,因此需要提供回调函数。当您的 get 仍在处理中时,您可能正在尝试使用该数组,可能是在实际填充之前。