【问题标题】:Why arent the functions activating in order? [duplicate]为什么功能没有按顺序激活? [复制]
【发布时间】:2019-06-02 15:39:48
【问题描述】:

所有功能都以随机顺序激活,我不知道为什么

我尝试对它们重新排序并将它们放置在不同的功能中。出于某种原因,设置警报可以工作并使它们保持有序,但这不是我想要的结果。大部分代码只有 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()

【问题讨论】:

  • 除了您的问题的答案(可以在我标记的副本中找到)之外,您可以通过使用带参数的函数来大量减少此代码。研究 DRY 原理

标签: jquery


【解决方案1】:

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

【讨论】:

  • 是的,我也是这么想的,有没有办法确保所有响应都按顺序返回?
  • 这不是一个有趣的方式,但是你可以在它的最后一个函数的响应函数中调用每个next函数,就像一个链。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
相关资源
最近更新 更多