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

时间:2022-05-01 23:33:36

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调用中向其添加元素。从我在互联网上看到的内容来看,我应该可以做到这一点!以下代码给出了错误:“无法调用未定义的方法'播放'”。播放列表[0]确实在函数内设置,警告$ .get调用中的播放列表[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!

3 个解决方案

#1


2  

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

有可能,播放列表在$ .get返回之前被使用 - 因为ajax调用是异步的。它在成功回调中起作用,因为一旦请求完成就会被触发,因此它将包含您期望的数据。

#2


6  

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.

你不必做任何这些。我的项目遇到了同样的问题。你要做的是在on success success回调中进行函数调用以重置全局变量。只要你将异步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 .

希望这对你有所帮助。

#3


1  

.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仍在进行中,但你可能正在尝试使用该数组,可能在实际填充之前。

#1


2  

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

有可能,播放列表在$ .get返回之前被使用 - 因为ajax调用是异步的。它在成功回调中起作用,因为一旦请求完成就会被触发,因此它将包含您期望的数据。

#2


6  

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.

你不必做任何这些。我的项目遇到了同样的问题。你要做的是在on success success回调中进行函数调用以重置全局变量。只要你将异步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 .

希望这对你有所帮助。

#3


1  

.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仍在进行中,但你可能正在尝试使用该数组,可能在实际填充之前。