请求后等待 JQuery 发送另一个

Wait after request to send another one in JQuery

我正在使用 spotify WEB API,我的想法是在单击下一首歌曲按钮时用歌曲名称更新 html。

问题是,当我点击按钮时,它确实会更新歌曲,但会更新到播放的最后一首,而不是当前播放的那首。

我尝试使用其他方式,例如 "success" 和 "complete" 参数,但是当使用 "complete" 时它不会更新并且在 "success" 上同样会发生。

我是否可以为下一次请求设置 "wait" 时间?

document.getElementById('skip-song').addEventListener('click', function(){
  $.ajax({
    type: 'POST',
    url:'https://api.spotify.com/v1/me/player/next',
    headers: {'Authorization': "Bearer " + access_token},
    }).done(function(data){
      $.ajax({
        url: 'https://api.spotify.com/v1/me/player/currently-playing',
        headers: {
          'Authorization': 'Bearer ' + access_token
        },
      }).done(function(response){
            currentPlayingPlaceholder.innerHTML = currentPlayingTemplate(response);
    });
  });
}, false);

通过引入超时,您可以在请求当前播放的歌曲之前等待几秒钟。不知道你还要等多久,感觉有点hacky。如果跳过函数返回当前正在播放就好了...

document.getElementById('previous-song').addEventListener('click', function() {
  $.ajax({
    type: 'POST',
    url: 'https://api.spotify.com/v1/me/player/previous',
    headers: {
      'Authorization': "Bearer " + access_token
    },
  }).done(function(data) {
    setTimeout(
      $.ajax({
        url: 'https://api.spotify.com/v1/me/player/currently-playing',
        headers: {
          'Authorization': 'Bearer ' + access_token
        },
      }).done(function(response) {
        currentPlayingPlaceholder.innerHTML = currentPlayingTemplate(response);
      }), 3000); //wait 3 seconds before updating the currently playing
  });
}, false);