Monday 15 September 2014

How can I do loop to a setInterval? (with array) - javascript -



How can I do loop to a setInterval? (with array) - javascript -

in code i'm trying draw rectangle @ time help of matter.js plugin. effect want, , have this: http://gyazo.com/0b84f082c4518c7c42a1651173e7b961 i'm getting database how many rectangles should draw ( in variable entradas[] ). have many positions in array, 1 says how many rectangles should draw. timer "desenha" draw 1 rectangle one, entradas[].

my problem can't create loop for, when draws rectangles first row, draw next rectangles sec row, , on.. draws rectangles first row.

that's part of code seek it:

var li=1; desenha=setinterval( function() { if(categoria[li]){ entra=entradas[li]; tex=texturas[li]; } if(rects<entradas[li]){ world.add(_world, bodies.rectangle( math.round(common.random(300,400)),-20,math.round(common.random(20,30)),math.round(common.random(20,30)), { render: { strokestyle: '#ffffff', sprite: { texture: tex } } }) ); } } , 500); contagem=setinterval( function(){ if(rects<entradas[li]){ rects=rects+1; document.getelementbyid("mytext").value = rects; rectsmoney=rects*valor[1]; document.getelementbyid("mytext2").value = math.round(rectsmoney); } },500); if(rects>=entradas[li]){ rects=0; li++; }

with help of echo know prints every row entradas[];

anyone can help me create loop works?

wrap setinterval functions , give them temporary scope.. desenha global variable, reassign desenha when looping. improve way maintain global array store intervals.

(function(li) { desenha[li] =setinterval( function() { if(categoria[li]){ entra=entradas[li]; tex=texturas[li]; } if(rects<entradas[li]){ world.add(_world, bodies.rectangle( math.round(common.random(300,400)),-20,math.round(common.random(20,30)),math.round(common.random(20,30)), { render: { strokestyle: '#ffffff', sprite: { texture: tex } } }) ); } } , 500); })(li);

javascript arrays loops setinterval

No comments:

Post a Comment