【问题标题】:JavaScript - slow down a loopJavaScript - 减慢循环
【发布时间】:2017-06-29 16:51:58
【问题描述】:

我在控制器中有这些代码,它们使用 $http get 调用 webservice 来检索一些数据。下面是代码:

更新代码

   var boolGetBoothCoords = false;
      BoothDesignatedCoordsService.getBoothDesignatedCoords(strListShortListedBooth[i], 3)

 .then(function(response) {
        var data = response.data
        console.log('data', data.arrBoothDesignatedCoords)
        boothDesignatedCoords = data.arrBoothDesignatedCoords;
        boolGetBoothCoords = true;
})

console.log("boothDesignatedCoords ", boothDesignatedCoords ); // undefined
// And a lot of other codes

但是,由于 $http get 是异步方法,程序会立即调用控制台日志和后面的代码,并且还没有定义 standDesignatedCoords。我不要那个。我希望程序仅在 Web 服务消费完成后才调用控制台日志和代码。所以我使用这个答案做了以下事情:how to slow down a javascript loop:

    go();
    function go() {
        console.log("hello");

        if (boolGetBoothCoords == false) {
            setTimeout(go, 1000);

        }
        else
        {

        }
    }
    go()

   console.log("boothDesignatedCoords ", boothDesignatedCoords ); // undefined
   // OTHER CODES that uses variable boothDesignatedCoords will be undefined     as well

但是,我不知道为什么它仍然会调用控制台日志,但是尽管使用了这种方法,但是Web服务消费还没有完成。有人可以帮帮我吗?谢谢。

【问题讨论】:

  • 为什么不在最后一个then 回调中包含console.log,在fnProceedOtherTask1() 调用旁边?
  • 很抱歉造成混淆。我已经更新了我的问题
  • 仍然,最好在最后一个回调中打印它而不是使用 setTimeout 等待...如果服务器响应时间超过 1 秒会发生什么?它仍然是未定义的
  • 我想我误解了 setTimeOut 的工作原理。你能向我解释一下 setTimeOut 在这种情况下是如何工作的吗?
  • 哦,我明白你在这里要做什么......我会写一个答案

标签: javascript jquery angularjs timeout settimeout


【解决方案1】:

setTimeout 是异步的,所以实际上调用go 函数并没有什么不同。

会发生什么:

  • 调用go()函数
  • 在函数内部调用setTimeout - 这将安排go 在(大约)1 秒内被调用
  • 之后立即致电console.log

您可能想要的是将您的console.log 像这样放在then 回调中:

var boolGetBoothCoords = false;
BoothDesignatedCoordsService.getBoothDesignatedCoords(strListShortListedBooth[i], 3)
  .then(function(response) {
    return response.data.arrBoothDesignatedCoords;
  })
  .then(function(boothDesignatedCoords) {
    console.log("boothDesignatedCoords ", boothDesignatedCoords );
  });

另一个选项(不推荐)是将console.log 放在if 函数中if 语句的else 部分中。

在这种情况下,您还应该在整个代码 sn-p 之前定义boothDesignatedCoords

【讨论】:

  • 您好,但是如果我按照您的代码操作,还有另一个问题。在.then(function(boothDesignatedCoords)方法被调用之前,程序已经退出了控制器
  • 我猜你正在使用 AngularJS 并且你想更改你的 $scope... 那么我建议你也应该在回调中对其进行所有更改
【解决方案2】:

应该在响应之后运行的代码,应该在响应之后调用。

var boolGetBoothCoords = false;

BoothDesignatedCoordsService.getBoothDesignatedCoords(
    strListShortListedBooth[i], 3)

.then(function(response) {
    var data = response.data
    console.log('data', data.arrBoothDesignatedCoords)
    boothDesignatedCoords = data.arrBoothDesignatedCoords;
    boolGetBoothCoords = true;
    codeThatUsesTheResponseData();
});
function codeThatUsesTheResponseData() {
    console.log("boothDesignatedCoords ", boothDesignatedCoords ); // undefined
    // And a lot of other codes
}

【讨论】:

  • 您好,但是如果我按照您的代码操作,还有另一个问题。在调用codeThatUsesTheResponseData方法之前,程序已经退出了控制器
  • 它在哪一行之后退出以及如何退出?在某些时候应该有返回、异常或刷新导致代码不调用下一行。
猜你喜欢
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-20
  • 2022-12-03
相关资源
最近更新 更多