简体   繁体   English

Javascript:如何使用 ajax 请求在 for 循环内以正确的顺序将对象推入数组

[英]Javascript: How do I get an objects to push in an array in the right order inside a for loop with an ajax request

So I have this script that connects to a video service api and I am trying to push the video titles that are served up in the ajax request in the order of the video_codes array.所以我有这个脚本连接到视频服务 api 并且我试图按 video_codes 数组的顺序推送 ajax 请求中提供的视频标题。 So these video codes are for seperate videos that give out information such as titles from the api ( 3t7l1ok4ba gets alicia , 5cj3ro2ujq gets drew , etc.)因此,这些视频代码用于单独的视频,这些视频提供诸如 api 中的标题之类的信息( 3t7l1ok4ba得到alicia5cj3ro2ujq得到drew等)

    // these codes connect to the api and get data
    var video_codes = [
    '3t7l1ok4ba',
    '5cj3ro2ujq',
    'ztodsfeifx',
    'u4g78mwyee'
]

// this is to hold the video titles provided in the api
var names = [];

// for loop makes the different requests
for (i = 0; i < video_codes.length; i++){
    (function(i) {
        var video_request = new XMLHttpRequest()
        video_request.open('GET', 'https://api.wistia.com/v1/medias/'+video_codes[i]+'.json?api_password=', true)
        video_request.onload = function() {
          var video = JSON.parse(this.response)
          var video_name = video.name;

          // add to names array
          names.push(video_name);
          // console log names array 4 times
          console.log(names)
        }
        video_request.send()
    })(i);
}

the script works in the sense that it pushes the names to the name array and console logs but they dont correspond to the order of the video_codes array.该脚本的工作原理是将名称推送到名称数组和控制台日志,但它们不对应于 video_codes 数组的顺序。

So for example 3t7l1ok4ba pushes out alicia but sometimes alicia will be the second, third, or fourth entry.因此,例如3t7l1ok4ba推出alicia但有时alicia将是第二个、第三个或第四个条目。 It is completely random i get different results everytime I run the script.这是完全随机的,每次运行脚本时我都会得到不同的结果。 Any help would be greatly appreciated!任何帮助将不胜感激!

The problem is the order the console logs is the order that the api responds.问题是控制台日志的顺序是 api 响应的顺序。 You are making all requests at the same time, so the second request may return before the first.您同时发出所有请求,因此第二个请求可能会在第一个请求之前返回。 One way to maintain the order is doing the next request only after the previous returned:维持顺序的一种方法是仅在前一个返回后才执行下一个请求:

function loadVideo(i) {
  if (i < video_codes.length) {
    var video_request = new XMLHttpRequest()
    video_request.open('GET', 'https://api.wistia.com/v1/medias/'+video_codes[i]+'.json?api_password=', true)
    video_request.onload = function() {
      var video = JSON.parse(this.response)
      var video_name = video.name;

      // add to names array
      names.push(video_name);
      // console log names array 4 times
      console.log(names);
      loadVideo(i + 1); // Load next only after response received
    }
    video_request.send()
  }
}

loadVideo(0); // Load first video

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM