-2

So, here's my problem... I've got a for loop inside a function

var fan1 = function () {
for(var i=0; i<flare1base.length; i++) {            
    for(var i=0; i<200; i++)
        flare1base.rotation.z += 0.01;
    };
};

It should do essentially that, fairly simple, but... I need it to wait 10 milliseconds before adding to the rotation again. The problem is I don't think I can use setTimeout or just use setInterval instead of the entire for loop, because it's acting on an object in an array, and if I do

f1 = setInterval("flare1array[i].rotation.z += 0.01",10);
setTimeout("clearInterval(f1)",2000);

It queues up an action to do rotate the thingy, but by the time the action occurs the for loop has gone around again and "i" is different.

  • 1
    Why do you have 2 for loops? Also, the use of `i` in both will screw things up. And why not just use 1 loop with `i < flare1base.length * 200`? – putvande Jul 31 '14 at 15:51
  • I think it's flare1base[i].rotation.z += 0.01 where i is the outer i – Carlo Moretti Jul 31 '14 at 15:54
  • I have two for loops because... the first (outer) for loop is what I use to make sure the following runs for every item in the array. The second (inner) for loop is what I'm attempting to use to replace the setInterval and setTimeout stuff that I have, since I can't use setInterval inside a for loop since by the time the interval executes, "i" has changed. – JupiterFlux Jul 31 '14 at 16:19
  • First problem I see is you reuse `i`. Should have `k` or `j` for the inner loop so it doesn't change on you. – Joseph Marikle Jul 31 '14 at 18:41

2 Answers2

0

See jQuery: Wait/Delay 1 second without executing code

Have you tried:

setTimeout(function (){

         flare1array[i].rotation.z += 0.01;

         }, 10); 
Community
  • 1
  • 1
  • I admit, I've not tried that specifically, but... again, that would replace the inner (second) for loop I have, but it would still be inside of the outer (first) for loop, so... still back at the problem of not being able to use setTimeout or setInterval inside a for loop. – JupiterFlux Jul 31 '14 at 16:49
0

How about using setInterval and then clearInterval? You could do something like this:

function rotate(max) {            
    var i = 0;
    return function () {
        flare1base.rotation.z += 0.01;         
        if (++i == max) clearInterval(id);        
    }
}

var id = setInterval(rotate(200), 10);

Similar example on JSFiddle

setInterval returns an id that can later be passed to clearInterval to prevent the action from continuing indefinitely. Here I've wrapped a counter variable i in a closure, which keeps track of how many times the inner function has been called. When it has been called max times, it stops.

By the way, if you know what the starting value of flare1base.rotation.z is, it would be better to calculate it afresh each time the function is called rather than continuously adding 0.01, as the result of repeated floating point additions may be imprecise. For example, if you know it starts at, you could do flare1base.rotation.z = 0.01 * ++i; (and remove the increment from the if statement).

To extend this to an array of items, you can wrap the whole process in a loop. Assuming that your items are in an array arr:

function rotate(arr, idx, max) {    
    var i = 0;
    return function () {
        arr[idx] += 0.01;         
        if (++i == max) clearInterval(ids[idx]);        
    }
}

var ids = new Array(5);    
for (var i = 0; i < 5; ++i) {    
    ids[i] = setInterval(rotate(arr, i, 200), 10);
}

updated JSFiddle

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • I could do that, but... again, that doesn't solve the problem of repeating an action, whether it's using a for statement or setInerval, inside of a for loop. Basically I need to do something a certain number of times with a certain amount of time between repetitions, only I don't need to do it once, I need to do that for each item in an array. – JupiterFlux Jul 31 '14 at 17:34
  • @JupiterFlux Please see my update which shows how you could apply the same principle to an array of items. – Tom Fenech Jul 31 '14 at 17:48
  • @JupiterFlux does this still not do what you want? – Tom Fenech Aug 02 '14 at 16:21