开发者

How to handle the "a script on this page is causing internet explorer to run slowly" alert?

开发者 https://www.devze.com 2023-02-25 08:23 出处:网络
From javascript I\'m calling a web method. With IE, if I pass a huge parameter to that web method, an alert \"Stop running this sciprt? A script on开发者_高级运维 this page is causing internet explore

From javascript I'm calling a web method. With IE, if I pass a huge parameter to that web method, an alert "Stop running this sciprt? A script on开发者_高级运维 this page is causing internet explorer to run slowly" pops up.

Is it possible to handle the click on the "Yes" button, so that if the user decides to cancel the script execution I can run some alternative script (in this case, my "alternative" script consists in closing some progress bar I popup just before running the long time script).

I have seen many posts explaining how to prevent this alert from being displayed, but I don't want to stop the alert from being displayed: I just want to be able to handle the case in which the user decides to stop the script execution.


I've dealt with this before on an internal app where they didn't care how long it took the browser to crunch the numbers, they just didn't want to have to click the prompt.

The key is to break the work down into relatively predictable chunks of work (predictable in terms of CPU time) and run them on a setInterval like:

function doWork(begin, end) {
    // Some chunk of what your worker function normally does from begin to end

    if (actualEnd < end) // Nothing left to do
        clearInterval(Interval);
}

var Interval = setInterval(doWork, 15);

This prevents the IE prompt from appearing (or Chrome from presenting the "Freeze" dialog). The next step is to add some code that lets the user skip it entirely; if the amount of work is known at the beginning, ask them right away. If not, start processing and after n chunks ask them if they'd like to do the cheaper function.

There are 2 other options for getting this much work done:

  1. Look ahead as to how much work there is to be done, and if it's a lot, pass it to the server (this unfortunately means writing your JS again on the server; of course, you could use a server engine that runs Javascript to save yourself some coding).
  2. Use background workers from Google Gears/HTML5

Finally, if you are doing this much work on demand, there are probably speed-up opportunities in the work you're performing - the data could be indexed beforehand on the server to make the calculations faster/simpler, things like that. But I don't know what you're calculating.

0

精彩评论

暂无评论...
验证码 换一张
取 消