Javascript timed array iteration

Sometimes you need to iterate on an array with a time delay between each iteration. For example, you have a list of UI elements. And you want to perform animation or process each components in a way to make it visible to user. If you dont use any delay there will be no UI feedback. User will think everything happened at once. Its better to provide an UI feedback on the work in progress.

Timed array iteration means you use a delay on each iteration. JavaScript has no sleep method. So its not possible to add delay like other languages. I used `window.setTimeout ` to achieve this. This is only available on browser based JavaScript. In server side JavaScript like nodejs, v8, rhino its not available. Here is the function.

function arrayWalkTimed(arr, dur, func){
    var p = arr[0]? arr[0]: null;
    if(p){
        func(p);
        window.setTimeout(function(){
            timed_array_walk(arr.slice(1), dur, func);
        }, dur);
    }
}

Author: shiplu

The Lone Explorer.

2 thoughts on “Javascript timed array iteration”

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve * Time limit is exhausted. Please reload the CAPTCHA.