简体   繁体   English

setInterval JavaScript的剩余时间

[英]time remainder of setInterval JavaScript

I want to be able to run code on the condition that there is time left on a setInterval. 我希望能够在setInterval上还有时间的情况下运行代码。

  let car = setInterval(function() {
    // stuff
  }, 2000);

  let vehicle = setInterval(function() {
    train()
  }, 1000);

  function train() {
    // stuff
    // and if car has time left, do this stuff too.
  }

At the last line of the train function, I want to check the car's time remaining but don't understand how to go about such a concept. 在火车功能的最后一行,我想检查汽车的剩余时间,但不了解如何实现这样的概念。 I guess knowing the exact time left or simply that there is time left is the same to me so which ever is easier. 我想知道剩下的确切时间,或者仅仅是剩下的时间对我来说都是一样的,所以哪一个更容易。

This is not going to be possible. 这是不可能的。 Timers are actually executed outside of the JavaScript runtime and the time specified in them is never a precise measurement - - it's actually the minimum time that the timer could be expected to run. 计时器实际上是在JavaScript运行时之外执行的,它们中指定的时间绝不是精确的度量-实际上,这是计时器可以运行的最短时间。 The actual amount of time depends on how busy the JavaScript runtime is and if the runtime is idle at the timer's timeout, the timer's callback function can be run. 实际时间取决于JavaScript运行时的繁忙程度,如果运行时在计时器超时时处于空闲状态,则可以运行计时器的回调函数。

So, essentially, you'd have to calculate how much longer the JavaScript execution stack will need in order to become idle, which you can't do because in order to get that time measurement, you have to execute the remaining code. 因此,从本质上讲,您必须计算JavaScript执行堆栈需要多少时间才能变为空闲状态,而这是无法做到的,因为要进行时间测量,必须执行其余代码。 So, you can't get the answer until there's no time left. 因此,直到没有时间,您才能得到答案。

But, based on your code, it seems that you could just set a simple "flag" variable that gets set when car runs. 但是,根据您的代码,似乎您可以设置一个简单的“ flag”变量,该变量在car行驶时设置。 So, inside train you can check to see if that flag has been set yet and if not, run the train code and then reset the flag. 因此,在train内部,您可以检查是否已设置该标志,如果尚未设置,请运行train代码,然后重置该标志。

Remember that timers are potentially quite imprecise, and that setInterval 's rules are...interesting...if your handler runs past when the next interval was meant to start. 请记住,计时器可能非常不精确,并且setInterval的规则很有意思……如果您的处理程序在下一个间隔应该开始时就过去了,那么... So important to keep in mind. 要牢记如此重要。

But if your goal is to know how long it's been since the start of the call to the timer vs when the timer is going to be set to fire again, it's a matter of remembering when you started 但是,如果您的目标是要知道自从开始调用计时器以来已经过了多长时间,以及何时将计时器重新设置为触发时间,则要记住您何时开始计时

var start = Date.now();

...and then later checking how long it's been relative to the interval. ...然后再检查相对于间隔的时间。 For instance: 例如:

if (Date.now() - start >= 1900) {
    // It's been at least 1900ms since the start of the `vehicle` call,
    // so based on a 2000ms interval, in theory you have only 100ms left
}

But , again, note that timers can be quite imprecise if other things are keeping the UI thread busy. 但是 ,再次,请注意,如果其他原因导致UI线程繁忙,则计时器可能会非常不精确。

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

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