使用循环为一个函数设置多个超时 [英] Using a loop to set multiple timeouts for a function

查看:92
本文介绍了使用循环为一个函数设置多个超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Moment.js 处理时间.已正确定义了10个临时对象(持续时间)的开始和结束时间,如这个问题是JSFiddle .

I'm using Moment.js to handle time. 10 Inning objects (durations) have properly been defined with start and end times, as shown in this question's JSFiddle.

该脚本旨在使用 差异 结束时间和存在之间定义一个要调用的功能endInning()的必要Timeout.这是在处理10个Innings的循环中实现的.

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. This is implemented within a loop to handle the 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);

}

但是,每个延迟都是相同的,而不是导致延迟增加12小时.

However, instead of resulting in delays that increment by 12 hours, each delay is the same.

结果:

  • 星期五(下午12:00), 412712000 (从现在)开始的第一局结束.

  • Ending Inning 1 on Friday, 12:00 PM, 412712000 ms from now.

星期五(下午12:00), 412712000 毫秒(现在)结束第二局.

Ending Inning 2 on Friday, 12:00 PM, 412712000 ms from now.

星期五(下午12:00), 412712000 毫秒(现在)结束第三局.

Ending Inning 3 on Friday, 12:00 PM, 412712000 ms from now.

...等等,直到Inning 10.

...and so on, until Inning 10.

我的错误是什么,我该如何解决?

在使用此脚本提出与我的实践有关的问题之后,我认为这些问题/答案是相关的:

After asking questions related to my practices with this script, I think that these questions / answers are related:

为什么我不应该在Java循环中发挥功能?

所以我的问题变成了:我该如何申请这种做法适合我的具体情况?

So my question becomes: How can I apply this practice to my specific situation?

推荐答案

带有结束日期的实际问题不属于超时问题(但是,这仍然是它们的问题)

Actual problem with ending dates does not belong to timeouts (however, that's still a problem with them)

第一个-您创建了一个inning对象,而您需要创建10个

first - you created one inning object while you need to create 10

所以,移动

var inning = new Object();

在第一个for循环内,这样您将创建10个约束对象,而不是一个.

inside the first for loop, this way you will create 10 inning objects instead of one.

-您滥用了moment库对象

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

您只是修改了begin.moment 变量,而这并不是您要实现的目标!

you just modified beginning.moment variable which is not what you trying to achieve!

在javascript中,所有对象均通过引用传递. https://stackoverflow.com/a/16880456 /870183

In javascript, all objects are passed by references 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

第三-超时问题.对于每次超时,我们需要使用另一个x变量创建另一个函数

third - timeout problem. For every timeout we need to create another function with another x variable

对我而言,封闭是很难理解的,所以请继续尝试. https://stackoverflow.com/a/111200/870183

Closures was hard to understand for me, so keep trying. https://stackoverflow.com/a/111200/870183

让我们创建一个函数,该函数将返回另一个函数

Let's create a function which will return another function

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

然后我们将传递新功能,其中x将被锁定"为其值setTimeout

and then we will pass new function where x will be "locked" to its value to setTimeout

setTimeout(endInningFunc(x), timeTillEnd);

最后一件事,不要使用全局变量! http://www.webdevelopersnotes.com/tutorials/javascript/global_local_variables_scope_javascript.php3

last thing, don't use global variables! http://www.webdevelopersnotes.com/tutorials/javascript/global_local_variables_scope_javascript.php3

例如for (var x=0);

最后,是工作示例. http://jsfiddle.net/LmuX6/13/

finally, the working example. http://jsfiddle.net/LmuX6/13/

这篇关于使用循环为一个函数设置多个超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆