split loops

by joplomacedo

JavaScript

var arr = [], 
    max,

    testPopulateArray = function (arr) {
        var num = 0,
            j = 10000;

        for (; j--;) {
            num = Math.floor(Math.random() * 10);
            arr.push(num);
        }
    };

testPopulateArray(arr);
max = arr.length;


function splitLoop(arr, max, func, current_index) {
    var date = +new Date(),
        current_index = current_index || 0;

    for (; current_index++ < max;) {
        if (+new Date() - date > 99) {
            func( arr[current_index], current_index);
        } else {
            setTimeout(function () {
                splitLoop(arr, max, func, current_index)
            }, 25);
        }
    }
}

splitLoop(arr, arr.length, function ( val, index ) {
    arr[index] = val * 7 / 24 + 3;
});