// define these as global variables // or else you will get an error in ECMAScript 5/Strict mode var divWidth = 0, divHeight = 0, timer, queue = []; function growDiv() { // empty the queue queue.length = 0; // queue up some elements to animate queue.push(div1, div2); // animate the next element, if any runQueue(); } function runQueue() { if (queue.length > 0) { timer = setInterval(function () { animate( queue.shift() ); }, 20); } } function animate(div) { divWidth += 5; divHeight += 5; S(div).width = divWidth; S(div).height = divHeight; if (divWidth >= 100) { clearInterval(timer); divWidth = 0; divHeight = 0; runQueue(); } }