Skip to content Skip to sidebar Skip to footer

Using A Loop To Set Multiple Timeouts For A Function

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 t

Solution 1:

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

so, move

var inning = newObject();

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

second - you misused moment library object

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

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

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

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

functionendInningFunc(x){
    returnfunction () {
        endInning(x)
    }
}

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

setTimeout(endInningFunc(x), timeTillEnd);

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

for example, for (var x=0);

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

Solution 2:

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. example http://www.phpied.com/3-ways-to-define-a-javascript-class/

functionInning(x) {
    this.x= x;
}

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

Post a Comment for "Using A Loop To Set Multiple Timeouts For A Function"