Jump to content

rounding up a bunch of setTimeouts


ctoz

Recommended Posts

With a lot of help—see recent posts—I've arrived at a function which changes two styles on a single element:function boxDC (boxname,display,color) { box = document.getElementById(boxname); box.style.display = display; box.style.color = color;}This is to be applied to a bunch of divs at regular intervals. I can do it primitive:function fireOne() { boxDC('jim','block','white');setTimeout('boxDC(\'jon\',\'block\',\'white\') ' ,60);setTimeout('boxDC(\'tom\',\'block\',\'white\') ' ,120);}but the bunch is bigger than 3 divs. So it looks like an array is needed, and a loop, which is where I'm still having problems:function fireOne() {// 32 divs oneWhite = new Array("jim","jon","tom", ... thru to "bob");function boxDC (boxname,display,color) { box = document.getElementById(boxname); box.style.display = display; box.style.color = color; } for (i = 0; i < oneWhite.length; i++) {setTimeout('boxDC(\'boxname\',\'block\',\'white\')', 60) }}looks close, but no cigar so far.cheersCT

Link to comment
Share on other sites

Don't use loops, as then there will be no pause between your style changes, use setTimeout() itself to cause the function to run multiple times.

function boxDC (display,color,current) { //Why need boxname when you have oneWhite?oneWhite = new Array("jim","jon","tom", ... thru to "bob"); //32 elementsbox = document.getElementById(oneWhite[current]);box.style.display = display;box.style.color = color;if (current != oneWhite.length - 1) {current++;setTimeout("boxDC('" + display + "','" + color + "'," + current + ")",60);}}

And invoke it boxDC("block","white",0) where current always equals 0 (as you want to start at array index 0).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...