为什么不按顺序激活功能?

时间:2019-06-02 15:39:48

标签: jquery

所有功能都是随机激活的,我不知道为什么

我试图重新排序它们并将它们放置在不同的功能中。出于某种原因,设置警报可以正常工作并使它们保持秩序,但这不是我想要的结果。大多数代码只有console.logs和我分配他们到本地存储。 非常抱歉,代码太长了

function ironman() {
  var XHR = new XMLHttpRequest();
  XHR.onreadystatechange = function() {
    if (XHR.readyState == 4 && XHR.status == 200) {
      var info = JSON.parse(XHR.responseText)
      console.log(info.Title)

      var box = info.BoxOffice
      boxnum = box.replace("$", "")
      boxnum1 = boxnum.replace(",", "")
      box = parseInt(boxnum1.replace(",", ""))
      console.log(box)

      var IM = info.Ratings[0].Value
      var IM1 = IM.replace("/10", "")
      IM = parseInt(IM1)
      console.log(IM)
      var RT = info.Ratings[1].Value
      var RT1 = RT.replace("%", "")
      RT = parseInt(RT1)
      console.log(RT)
      var Meta = info.Ratings[2].Value
      var Meta1 = Meta.replace("/100", "")
      Meta = parseInt(Meta1)
      console.log(Meta)

      var ti = info.Title

      boxoffice.push(box)
      boxoffice1.push(box)
      title.push(ti)
      title1.push(ti)
      imbd.push(IM)
      imbd1.push(IM)
      rottentomatoes.push(RT)
      rottentomatoes1.push(RT)
      metacritic.push(Meta)
      metacritic1.push(Meta)
      console.log()

      localStorage.setItem("boxoffice", JSON.stringify(boxoffice))
      localStorage.setItem("boxoffice1", JSON.stringify(boxoffice1))

      localStorage.setItem("title", JSON.stringify(title))
      localStorage.setItem("title1", JSON.stringify(title1))

      localStorage.setItem("imbd", JSON.stringify(imbd))
      localStorage.setItem("imbd1", JSON.stringify(imbd1))

      localStorage.setItem("rottentomatoes", JSON.stringify(rottentomatoes))
      localStorage.setItem("rottentomatoes1", JSON.stringify(rottentomatoes1))

      localStorage.setItem("metacritic", JSON.stringify(metacritic))
      localStorage.setItem("metacritic1", JSON.stringify(metacritic1))
    }
  };
  XHR.open("GET", "http://www.omdbapi.com/?t=Iron Man&apikey=???????");
  XHR.send()
}

function hulk() {
  var XHR = new XMLHttpRequest();
  XHR.onreadystatechange = function() {
    if (XHR.readyState == 4 && XHR.status == 200) {
      var info = JSON.parse(XHR.responseText)
      console.log(info.Title)

      var box = info.BoxOffice
      boxnum = box.replace("$", "")
      boxnum1 = boxnum.replace(",", "")
      box = parseInt(boxnum1.replace(",", ""))
      console.log(box)

      var IM = info.Ratings[0].Value
      var IM1 = IM.replace("/10", "")
      IM = parseInt(IM1)
      console.log(IM)
      var RT = info.Ratings[1].Value
      var RT1 = RT.replace("%", "")
      RT = parseInt(RT1)
      console.log(RT)
      var Meta = info.Ratings[2].Value
      var Meta1 = Meta.replace("/100", "")
      Meta = parseInt(Meta1)
      console.log(Meta)

      var ti = info.Title

      boxoffice.push(box)
      boxoffice1.push(box)
      title.push(ti)
      title1.push(ti)
      imbd.push(IM)
      imbd1.push(IM)
      console.log(imbd1)
      rottentomatoes.push(RT)
      rottentomatoes1.push(RT)
      metacritic.push(Meta)
      metacritic1.push(Meta)
      console.log()

      localStorage.setItem("boxoffice", JSON.stringify(boxoffice))
      localStorage.setItem("boxoffice1", JSON.stringify(boxoffice1))

      localStorage.setItem("title", JSON.stringify(title))
      localStorage.setItem("title1", JSON.stringify(title1))

      localStorage.setItem("imbd", JSON.stringify(imbd))
      localStorage.setItem("imbd1", JSON.stringify(imbd1))

      localStorage.setItem("rottentomatoes", JSON.stringify(rottentomatoes))
      localStorage.setItem("rottentomatoes1", JSON.stringify(rottentomatoes1))

      localStorage.setItem("metacritic", JSON.stringify(metacritic))
      localStorage.setItem("metacritic1", JSON.stringify(metacritic1))
    }
  };
  XHR.open("GET", "http://www.omdbapi.com/?t=The Incredible Hulk&apikey=???????");
  XHR.send()
}

function thor() {
  var XHR = new XMLHttpRequest();
  XHR.onreadystatechange = function() {
    if (XHR.readyState == 4 && XHR.status == 200) {
      var info = JSON.parse(XHR.responseText)
      console.log(info.Title)

      var box = info.BoxOffice
      boxnum = box.replace("$", "")
      boxnum1 = boxnum.replace(",", "")
      box = parseInt(boxnum1.replace(",", ""))
      console.log(box)

      var IM = info.Ratings[0].Value
      var IM1 = IM.replace("/10", "")
      IM = parseInt(IM1)
      console.log(IM)
      var RT = info.Ratings[1].Value
      var RT1 = RT.replace("%", "")
      RT = parseInt(RT1)
      console.log(RT)
      var Meta = info.Ratings[2].Value
      var Meta1 = Meta.replace("/100", "")
      Meta = parseInt(Meta1)
      console.log(Meta)

      var ti = info.Title

      boxoffice.push(box)
      boxoffice1.push(box)
      title.push(ti)
      title1.push(ti)
      imbd.push(IM)
      imbd1.push(IM)
      console.log(imbd1)
      rottentomatoes.push(RT)
      rottentomatoes1.push(RT)
      metacritic.push(Meta)
      metacritic1.push(Meta)
      console.log()

      localStorage.setItem("boxoffice", JSON.stringify(boxoffice))
      localStorage.setItem("boxoffice1", JSON.stringify(boxoffice1))

      localStorage.setItem("title", JSON.stringify(title))
      localStorage.setItem("title1", JSON.stringify(title1))

      localStorage.setItem("imbd", JSON.stringify(imbd))
      localStorage.setItem("imbd1", JSON.stringify(imbd1))

      localStorage.setItem("rottentomatoes", JSON.stringify(rottentomatoes))
      localStorage.setItem("rottentomatoes1", JSON.stringify(rottentomatoes1))

      localStorage.setItem("metacritic", JSON.stringify(metacritic))
      localStorage.setItem("metacritic1", JSON.stringify(metacritic1))
    }
  };
  XHR.open("GET", "http://www.omdbapi.com/?t=Thor&apikey=???????");
  XHR.send()
}

ironman()
hulk()
thor()

1 个答案:

答案 0 :(得分:0)

我认为Ajax请求按顺序发送,但是它们的响应异步返回。因此,哪个请求较早返回响应取决于每个请求的服务器响应时间。