简体   繁体   English

使用循环为一个函数设置多个超时

[英]Using a loop to set multiple timeouts for a function

I'm using Moment.js to handle time. 我正在使用Moment.js处理时间。 10 Inning objects (durations) have properly been defined with start and end times, as shown in this question's JSFiddle . 正确定义了10个Inning对象(持续时间)的开始和结束时间,如该问题的JSFiddle所示

This script is meant to use the the difference between an Inning end time and the present to define the necessary Timeout to be set for a function endInning() to be called. 该脚本旨在使用Inning 结束时间与当前时间之间的来定义要为函数endInning()调用而设置的必要Timeout This is implemented within a loop to handle the 10 Innings. 这是在处理10个Innings的循环中实现的。

for (x = 0; x < 10; x++) { // for each of ten defined innings

    // calculate the difference between the end of inning x and now
    timeTillEnd = moment(Game.innings[x].start).diff(moment(now),"milliseconds");

    // and set the necessary delay
    setTimeout(function () { 
        endInning(x);
    }, timeTillEnd);

}

However, instead of resulting in delays that increment by 12 hours, each delay is the same. 但是,每个延迟相同,而不是导致延迟增加12小时。


The result: 结果:

  • Ending Inning 1 on Friday , 12:00 PM, 412712000 ms from now . 现在起 41,212000毫秒, 周五 12:00结束Inning 1

  • Ending Inning 2 on Friday , 12:00 PM, 412712000 ms from now . 现在 起到412712000 ms,在周五 12:00 PM结束Inning 2

  • Ending Inning 3 on Friday , 12:00 PM, 412712000 ms from now . 现在开始 41:212000 星期五 ,下午12:00结束Inning 3

  • ...and so on, until Inning 10. ...依此类推,直到Inning 10。


What's my mistake and how can I fix it? 我的错误是什么,我该如何解决?


Edits: 编辑:

After asking questions related to my practices with this script, I think that these questions / answers are related: 在用此脚本询问与我的实践相关的问题后,我认为这些问题/答案是相关的:

So my question becomes: How can I apply this practice to my specific situation? 所以我的问题变成了:如何将这种做法应用于我的特定情况?

function doSetTimeout(i) {
  setTimeout(function() { alert(i); }, 100);
}

for (var i = 1; i <= 2; ++i) {
  doSetTimeout(i);
}

copied it from setTimeout in for-loop does not print consecutive values I won't use it if I am looping too much as every function call is creating a new function object and it will be memory intensive if you are looping too much, alternative would be to create a class like structure. 在循环中setTimeout复制它不会打印连续的值 ,如果我循环太多,我将不使用它,因为每个函数调用都在创建一个新的函数对象,如果循环太多,它将占用大量内存,否则是创建类结构。 example http://www.phpied.com/3-ways-to-define-a-javascript-class/ 范例http://www.phpied.com/3-ways-to-define-a-javascript-class/

function Inning(x) {
    this.x= x;
}

Inning.prototype.onTimeOut = function() {
    // do your thing with this.x
};

Actual problem with ending dates does not belong to timeouts (however, that's still a problem with them) 结束日期的实际问题不属于超时(但是,这仍然是它们的问题

first - you created one inning object while you need to create 10 首先 -您创建了一个inning对象,而您需要创建10个

so, move 所以,移动

var inning = new Object();

inside the first for loop, this way you will create 10 inning objects instead of one. 在第一个for循环中,这样您将创建10个约束对象,而不是一个。

second - you misused moment library object 第二 -您滥用了moment库对象

inning.start = beginning.moment.add("hours", (inningHours * x)); //WRONG

you just modified beginning.moment variable which is not what you trying to achieve! 您只是修改了begin.moment变量,而这并不是您想要实现的!

In javascript, all objects are passed by references https://stackoverflow.com/a/16880456/870183 在javascript中, 所有对象均通过引用 https://stackoverflow.com/a/16880456/870183 传递

So, you must create new moment object and then modify it. 因此,您必须创建新的矩对象,然后对其进行修改。

inning.start = moment(beginning.moment).add("hours", (inningHours * x)); //correct

third - timeout problem. 第三 -超时问题。 For every timeout we need to create another function with another x variable 对于每个超时,我们需要使用另一个x变量创建另一个函数

Closures was hard to understand for me, so keep trying. 关闭对我来说很难理解,因此请继续尝试。 https://stackoverflow.com/a/111200/870183 https://stackoverflow.com/a/111200/870183

Let's create a function which will return another function 让我们创建一个函数,该函数将返回另一个函数

function endInningFunc(x){
    return function () {
        endInning(x)
    }
}

and then we will pass new function where x will be "locked" to its value to setTimeout 然后我们将传递新函数,其中x将被“锁定”到它的值 setTimeout

setTimeout(endInningFunc(x), timeTillEnd);

last thing, don't use global variables! 最后一件事,不要使用全局变量! http://www.webdevelopersnotes.com/tutorials/javascript/global_local_variables_scope_javascript.php3 http://www.webdevelopersnotes.com/tutorials/javascript/global_local_variables_scope_javascript.php3

for example, for (var x=0); 例如, for (var x=0);

finally, the working example. 最后,是工作示例。 http://jsfiddle.net/LmuX6/13/ http://jsfiddle.net/LmuX6/13/

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

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