Javascript How To Process Large Amount Of Work

If you have a large amount of work which for some reason you have to do it on the front-end, how can you chunk them up to avoid the "stop running script" error?

The below code is just a rough example

var handle_multi_edit_in_chunks_msgbox_visible = false;
function handle_multi_edit_in_chunks(ar, num, interval, initialQuantity) {
    // ar: is an array which represent the total amount of work that you have to do
    // num: the amount of work that you will do in a single setTimeout
    // interval: number of milliseconds specified for setTimeout
    // This function will call itself until ar.length returns 0.  
    var min = Math.min(num, ar.length);
    var i = 0;
    // Display the modal dialog if it is not already displayed.

    if (handle_multi_edit_in_chunks_msgbox_visible === false) {
        Ext.MessageBox.show({
            title: 'Processing',
            msg: '<span>Applying your change to ' + initialQuantity + ' question(s).  Remaining: ' + '<span id="progress">' + ar.length + '</span></span>',
            width: 400,
            icon: Ext.MessageBox.INFO,
            closable: false,
            fn: function(btn) {}
        });
        handle_multi_edit_in_chunks_msgbox_visible = true;
    }

    // Do the actual work
    for (i = 0; i < min; i++) {
        //handleParentChildEdit(store, newValue, originalValue, record);
    }
    console_log("min: " + min);
    ar.splice(0, min);
    Ext.get('progress',ar.length);

    if (ar.length > 0) {
        setTimeout(function() { handle_multi_edit_in_chunks(ar,num,interval,initialQuantity); }, interval);
    } else {
        // There is nothing left to do. Just close the modal dialog.
        Ext.MessageBox.hide();
        handle_multi_edit_in_chunks_msgbox_visible = false;
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License