Javascript execution blocking rendering/scrolling in IE 8 - javascript

I'm having a bit of trouble with IE 8 (and probably all previous versions). Firefox and Chrome/Webkit are seemingly fine.
Something is causing page rendering, scrolling, and basically all page interaction to become blocked. As best I can tell, Javascript execution is causing this to happen.
Specifically, I think there are two major responsible parties in my specific situation - Google Maps API (v3) and Facebook Connect.
In both cases, I am using the asynchronous load methods provided by both Google and Facebook.
I've tried a couple of things so far, to no avail:
Delaying execution with jQuery's $(document).ready(). This just prevents the locking until slightly later in the page load. Actually, since I use gzip compression, I'm not really sure it does anything - I'm not clear on how that works.
Delaying execution with window.onload. Same situation - the whole page loads, then it locks up while it grabs and executes the Facebook Connect code.
Using setTimeout(function(){}, 0). I'm not 100% clear on how this is supposed to actually work - as I understand it, it essentially is supposed to force the execution of the function's code to wait until the stack is clear. Unfortunately, this doesn't seem to do much of anything for me.
I think the problem is especially exaggerated for me because I am on a slow connection.
I can't think of any specific oddities with my site that would be a factor, but I won't rule that out.
So, my question:
Are there any best practices or existing solutions to this issue?
Is there something that I am obviously doing wrong?
The offending site is at: http://myscubadives.com/, if anyone would be willing to take a look at the specific implementation.
Thank you in advance for your time and help.
Sam

Yes, the browser (at least IE) suspends itself while Javascript is being executed. This makes things a bit faster, because it doesn't have to redraw and recalculate layout every time you make a change. However if your Javascript takes a long time to execute, this will seem like freezing. Synchronous XMLHttpRequests also count.
Unfortunately there is no pretty workaround. The typical advice is to use window.setTimeout() function with timeout set to 0 (or something very small) to split the workload in several parts. Inbetween the browser can manage to redraw itself and respond to some user interaction, so it doesn't seem frozen. The code gets ugly though.
In case of lengthy XMLHttpRequests you have no choice but to use the asynchronous version.
Added: Ahh, I see that you already know this. Should read more carefully. :P Did you also know that IE8 has Developer tools built in (Press F12 to activate) and that the Javascript tab has a profiler? I checked it out, and 2 seconds were spent exclusively in jQyery's get() method. Which gives me a strong suspicion that something is still using synchronous XMLHttpRequests.
Function: get
Count: 10
Inclusive time: 2 039,14
Exclusive time: 2 020,59
Url: http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js
Line: 127

Related

Is it possible to trigger Chrome's "Rats! WebGL hit a snag" accidentally?

From what I can tell about this message, it appears when the driver encounters an error. The implication seems to be that the JavaScript code can't trigger this warning even if there's some kind of mistake in it, because of higher-level error checking.
But my code recently triggered it, just once and I haven't been able to reproduce it (and the code — in theory at least — sends the exact same commands to WebGL every time). So I just want to know if I should be debugging my code (I can't see any obvious errors), or if I should put it down to a hardware random / browser bug.
I can post semi-reduced code on request, but it's still about half a thousand lines and I certainly don't expect people to debug it for me, hence the more general is-it-possible question.
I'm struggling a little with what your question means by "trigger". When this error occurs, javascript is always in the causal chain, because WebGL doesn't run without javascript executing it, right? So certainly it's possible to trigger the error accidentally (unless you always trigger it on purpose, which I'm sure is not the case).
However, graphics drivers are notorious for being affected by many factors that are not under programmer control or necessarily detectable by the programmer, and therefore seem non-deterministic. That's why Google recommends that you try reloading the page when the error occurs. (A link to their guidance was here, but they moved or removed the page.) I've often had Chrome successfully display some WebGL after reloading a page, when the previous attempt ended in the "snag" error. (And I know you're aware of that.)
There are certainly times when WebGL code (like the various shaders on http://shadertoy.com/) work fine on some machines and not others, and work fine one minute and not the next, on the same machine. So that suggests that the "snag" error doesn't necessarily imply that your code is at fault.
However, articles like How to write portable WebGL can help you minimize risky practices in your code, and reduce the frequency of these errors.

Chrome freezes on my backbone page: how to debug?

The project I'm working on involves a "planning" screen, which is entirely made with backbone.js (the other pages of the app are not).
My issue is that from times to times, chrome freezes and the web view stop responding to any interaction. Sometimes, I can manage to quit chrome itself, but usually the controls does not answer either.
I'm pretty convinced this is related to the js code. It seems to me that when a script takes too much time, or loops indefinitely, Chrome can detect this and interrupt the script. However, since this is not the case, I'm thinking that too many js objects stay in memory.
Whatever the cause is, I would like to know which chrome dev tools can help me here. While I'm not a beginner in js, asides setting breakpoints and calling console.log, I have no idea how to debug JS apps. I'm not opposed to use another browser if the dev tools are more suited.
Thanks a lot for your time !
FTR : This is a rails 3.2.8 app, using mongodb, & Backbone.js 0.9.2. The js code is written in coffeescript. This issue happened on my macbook air 2012 running mountain lion, as well as on the client machine which runs on windows 7. The issue appeared at least on chrome 22 & 23.
Using the Javascript CPU profiler, I was able to find the group of functions that seems to be responsible for the freezing.
I'm still open to any advice/ressource on debugging javascript code.
Make a console.log in the loop and check if its the same point freezing on all chrome versions. There is a limit see Browser Javascript Stack size limit.
Maybe add some code? Because there could be some memory leaks especially with event handlers!
What I would do is the long and weary approach to getting your hands dirty with console.log. --- in this case.
In your case, and THX for the hint by the way (finding the offender with the CPU profiler, I'll try that next time round), I guess the offenders might be some functions "callbacking" themselves .. probably via some sort of event-handling/bubbling/callback-combination.
What happens then usually is that it just doesn't recognize the endless-loop, because the callback-stack is kinda "broken". Therefor it will never throw a browser-error.
If you have any luck, it doesn't kill the browser quick enough to kill the console. I did have that at times, the console killed (remaining silent), the coffeescript files not even loaded in the debugger (I use a JIT-coffee-to-js-translator as in JS-MVC) ... the page frozen or not doing anything ...
So, if indeed you are lucky and the debugger spits out your console.logs you can thereby guess where your unwanted loop is hiding. Just by looking at the repeated order of the output statements.
of course you know you can set breakpoints right? even conditional breakpoints
I'm currently struggling a bit untangling a similar sounding loop - i guess in case of emergency some alert() calls in your code would at least slow the loop down
or maybe some wait calls (bad idea)
my loop is running so fast I loose my console log!

How to debug script that makes the page run slowly (according to firefox)?

I have a script that has a few timeouts, draws a menu using Raphael and uses all sorts of dynamic properties to accomplish this. You can take a look at it over at jsfiddle: JSFiddle version of my code
My problem is that Firefox, just occasionally, throws the "a script is running slowly etc." error when this page is open, might be after half a minute or more. Typically I'll have hovered over one element or so, so one sub menu is open at this time. The error usually doesn't point to any of my js files, sometimes even Firefox's own files.
How do I debug this, is it possible at all? Any tips are appreciated. (I'm using chronos for the timers now, didn't seem to help)
Profile your code
You probably want to do Profiling, i.e. performance analysis of your code. As others have pointed out, Firebug is a good tool in Firefox. More specifically: In Firebug, click Profile in the Console tab. Then play with your navigation a bit and click Profile again to finish the analysis and get the results. They will tell you which functions were called how often and how long their execution took. This will help you identify performance bottlenecks in your code.
Optimize
On a quick glance I would say you could probably still optimize the DOM queries. Save references to the results of your expensive queries. And use contexts if possible, to make queries more efficient.
Also, requestAnimationFrame() seems to be the way to go for javascript animations now, instead of setTimeout().
Most browsers have a mechanism of some sort to allow users to stop "long running scripts".
How a script is deemed to be "long running" varies slightly between browsers, and is either the number of instructions executed within an 'execution context' or its elapsed duration.
JavaScript, when running in a browser, is predominantly event-driven (except for the immediate execution of JavaScript as the page is being parsed). The browser will wait until an execution context has completed before doing anything, and any display refreshing etc can be blocked whilst waiting for JavaScript to execute.
You basically need to break up your instructions into responses to events. If you have a massive loop anywhere (e.g. to do an animation), you really need to use an interval to break up the execution cycles. E.g. var interval = window.setInterval(refreshDisplay, 50); - this will call a refreshDisplay function (with no arguments) every 50 milliseconds (a crude 20 calls-per-second).
Try Firebug and use something like this as shown in screenshot --
Link to download firebug --
http://getfirebug.com/downloads/
firebug for debugging javascript (Firefox addon)
Use F12 to show firebug
This will help you !
With Firebug you can debug javascript with breakpoints and see all ajax calls !
There's probably some operation that's running slowly, or one of your loops has a bad complexity. You can use binary splitting: remove half of your code, see if there's a dramatic increase in timing, and do the same in the half of the code that's running the slowest.

Unresponsive Script Error Jquery

I am in a middle of creating a website where we need to use a lot of script work. But due to extensive data load as we are loading the listing for youtube the browser hangs up due to unresponsive script error.
The data is being loaded using AJAX.
Can any one suggest how to tackle this issue?
Your suggestions will be highly appreciated.
Thanks in advance
J
You can change the dom.max_script_run_time and dom.max_chrome_script_run_time values, but it means the warning comes less often.
The best thing that you can do just find the root of the problem. I think you use Firefox, so if the script runs too slowly and long just click the Stop Script button and go to the error console.
More info are here.
Without knowing details, I can offer only these generalizations. Maybe one or more will help:
The jQuery .ajax call takes an option to make the call synchronous. That, combined with an unresponsive server, would cause the problem you describe. So, make sure you haven't mistakenly set that option.
On some browsers, FF at least, running out of memory causes an unresponsive script error. Chrome has a nice feature in which you can open a new tab, enter "about:memory" into the address bar, and a get a quick overview of how much memory other Chrome tabs/windows are using. See anything outrageous here?
On FF and IE, I have seen an infinite loop cause an unresponsive script error. Use a profiler, to detect this condition.
A profiler, like the one build in to Firebug for example, can help you pin down where in your script the problem is occurring. Knowing that, you can look for opportunities to break up the code into smaller operations. E.g., if you are processing a large amount of data, perhaps you could make a recursive call a setTimeout handler to process chunks of data at a time.

Can a Javascript error slow the website's load time

I have a web portal cricandcric.com which I have done using the php, java script, and mysql.
I dont see the java script error in Firefox, but i see the error in IE,
I observe the site loads faster in Firefox than in IE.
So my question, does java script errors can slow down the website loading time, even though the java script placed at the end of the page, (Yslow Strategies)
It depends. If the error happens early on and a lot of your script code is bypassed, it could actually make it faster. But every time an error occurs, there is some overhead (the exception object has to be built and sent up the call stack to look for any catches), so if it happens at the end, the script would run slower.
But I doubt your change in load time is noticeably impacted by script errors. How long the script takes to execute on the browser's JS engine or a host of other factors will have more impact.
IE's javascript engine has always been notably behind the other common browsers in performance, so it really might come down to that more than anything else. One of the many improvements in IE9 is JS execution speed that's actually competitive.
That said, the JS error probably is worth looking into, since it's occurring every time the image slideshow advances which happens once every couple of seconds.
If you're concerned about performance in general, there are a couple of tools like YSlow and the recently-opensourced DOM Monster bookmarklet, for giving suggestions on general ways to speed up websites. You might also want to check out some of the writings of Steve Souders.

Categories

Resources