a developer's notes – a semi-technical web development BLOG

September 5, 2011

To prevent IE from locking up on long running scripts

Filed under: Javascript / JQuery — Duy Nguyen @ 2:13 pm
Tags: , , , , , , , , ,

To prevent IE from locking up on long running scripts, break out your logic with the Javascript setTimeout method.

I was doing this but it was not working. This was not working because of the way I implemented the looping.


    var size = 100000000; //10,000,000 is ok, 100,000,000 is bad    
    var mystring = "";
    var chunkSize = size / 10000;
    var chunkSizeCounter = 0;

function button2_clientClick() {
          
        for (var i = 0; i < size; i++) {
            AddToMyString();
        }

        return false;
    }


    function AddToMyString() {
        if (chunkSizeCounter == chunkSize) {
            chunkSizeCounter = 0;
            setTimeout("AddToMyString()", 100);
        }

        mystring = mystring + ",";
        chunkSizeCounter++;   
    }

But I need to do this. This allowed the browser to continue to loop and process without it locking up. This is because the setTimeOut function runs on a parallel process, which allowed the browser to not “wait” for the processing to complete. Unlike the previous loop, it was taking too long for the For loop to complete. The second implementation allowed the concatString() function to complete on each interval.

    var size = 100000000; //10,000,000 is ok, 100,000,000 is bad    
    var mystring = "";
    var chunkSize = size / 10000;
    var chunkSizeCounter = 0;

    function button2_clientClick() {
        
        concatString();
        return false;
    }

    function concatString() {
        mystring += ",";
        chunkSizeCounter++;
        if (chunkSizeCounter < size)
            window.setTimeout(concatString, 10);
    }
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: