简体   繁体   中英

a script on this page is causing ie to run slowly

The problem is in the title - IE is misbehaving and is saying that there is a script running slowly - FF and Chrome don't have this problem.

How can I find the problem . .there's a lot of JS on that page. Checking by hand is not a good ideea

EDIT : It's a page from a project i'm working on... but I need a tool to find the problem.

End : It turned out to be the UpdatePanel - somehow it would get "confused" and would take too long to process something. I just threw it out the window - will only use JQuery from now on :D.

And I'm selecting Remy Sharp's answere because I really didn't know about the tool and it seems pretty cool.

Long running scripts are detected differently by different browsers:

  • IE will raise the warning once 5 million statements have been executed ( more info on MSDN )
  • Firefox will warn if the script takes longer than 10 seconds ( more info on MDN )
  • Safari will warn if the script takes longer than 5 seconds
  • Chrome (1.0) has no set limit and will simply keep trying until an OutOfMemory exception at which point it crashes
  • Opera will just continue to run forever, without warning.

Nicholas Zakas has written an excellent article covering this topic.

As such - the best way to avoid these problems is by reducing looping, recursion and DOM manipulation.

Get yourself a copy of the IBM Page Profiler:

https://www.ibm.com/developerworks/community/groups/service/html/communityview?communityUuid=61d74777-1701-4014-bfc0-96067ed50156

It's free (always a win). Start it up in the background, give it a few seconds, then refresh the page in IE. Go back to the profiler and it will list out all the resources used on the page and give you detailed profile information - in particular where JavaScript is taking a long time to execute.

It should be a good start to finding the source of your problem.

If the script tags are inline, I'd suggest creating a local copy of the file and separating out the script tags to separate files if you can.

Remove half the code and see if it still happens. If not, it's in the half you removed. Repeat until you figure out what code block is causing the problem.

It is usually an infinite loop that causes this. Check your loops and their exit conditions.

I found that adding alert('before X') alert('after X') was helpful for me to find my issue. I added them to my $(function () {

}

For IE, the dialog is based on the # of JS commands processed. See here for info & method to change default: http://support.microsoft.com/kb/175500

you can also check if there is a google analytics javascript include in your page. The bug occured only with IE and once the google code was removed, it worked!

Make sure that below JavaScript code is run only once:

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(win_onload);

Above code is inside the function win_onload() .

In my case having too many ASP.NET HoverMenuExtender controls in a GridView bound to a lot of rows was causing extremely slow performance. I removed the HoverMenuExtender and my speed issues (and the dialog) went away. Not quite related to why the dialog pops up, but it might help someone.

I don't believe there's a tool that can find the offending script. You might try attaching an IE debugger like Visual Studio and maybe it will break at the point where the problem is occurring. But I can't give any guarantees on that working.

In the past when I've had similar problems I've simply commented out sections of code to test narrow down where the issue is occurring, usually in a binary search type pattern. Comment out half of the javascript libraries, etc...

Aside from that as others have said, this type of problem occurs from large loops and many setTimeout function calls or setTimeout recursive loops.

If javascript ties up page processing for more than 10 seconds, you get this message. IE obviously has a slower javascript engine, causing this.

I'm guessing that some code optimization will certainly help, and try reducing the amount of javascript executing on page load. Perhaps use setTimeout() to defer processing of some unnecessary things if you have to.

As far as tools go, use Firebug's profiler to see where you're spending so much time.

@nikmd23 answer is informative..

My 2 cents here... I had the script warning issue - due to loading a dropdown list items with the help of for loop.

I referred @Eric Leschinski post in Disabling the long-running-script message in Internet Explorer .

Refer setTimeout for loading items in a dropdown list to see how I resolved this problem

There are few reason for this kind of alert

  1. No. of JS instructions executed by IE exceeds predefined limits. This can be fixed by editing windows registry see Here

  2. Optimize the javascript code so that execution time is reduced.

  3. JS code optimization is a real trial and error subject and there are few thumb rules to do so. Just Google it.

If you have control over the JavaScript, you could break it into separate scripts or try a Lazy Load approach.

Just my $.02

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM