How can I give control back (briefly) to the browser during intensive JavaScript processing?

后端 未结 4 647
孤城傲影
孤城傲影 2020-11-27 03:15

I have read the post here about using setTimeout() during intensive DOM processing (using JavaScript), but how can I integrate this function with the below code? The below c

4条回答
  •  情话喂你
    2020-11-27 03:44

    If you need something simpler, I wrote a jQuery plugin to ease writing of asynchronous loops: jQuery Async.

    Using the plugin, your code can be rewritten as:

    function appendToSelect() {
        $("#mySelect").children().remove() ;
        $("#mySelect").html(
            ''
        );
    
        /////////////////////////////
        var i = 1;
        $.whileAsync({
            test: function(){ i < obj.data.length; }
            loop: function()
            {
                $("#mySelect").append(
                    ''
                ); 
                i++;
            }
        });
        /////////////////////////////
    }
    

    Should help the responsiveness. Tweak the 'bulk' and 'delay' option for more control.

提交回复
热议问题