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.
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);
}
However, instead of resulting in delays that increment by 12 hours, each delay is the same.
The result:
Ending Inning 1 on Friday, 12:00 PM, 412712000 ms from now.
Ending Inning 2 on Friday, 12:00 PM, 412712000 ms from now.
Ending Inning 3 on Friday, 12:00 PM, 412712000 ms from now.
...and so on, until 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?