If you're having difficulty finding and diagnosing infinite loops by throwing console.log
calls into all your suspect loops, try using alert
and confirm
instead. While console.log
can be somewhat uncooperative (or, by the sounds of it, completely non-functional) when the browser is stuck in an infinite loop, alert
will always work immediately.
With confirm, you can even go one step better and have the option of breaking out of the loop. For example, here is your example code modified to let you break out of the loop at any time.
jQuery(document).ready(function ($) {
var i = 0;
while(i < 10){
if (confirm("i = " + i + "; break the loop?")) {
throw new Error("Loop broken by manual user override");
}
$('#someAwesomeDiv').append('<br>i</br>')
i--;
}
});
(Fiddle).
Then if you hit the infinite loop, you can choose to throw an error and hopefully (unless it gets caught, or the same code is immediately triggered again) regain control of the window and be left free to play around in the window and the console and try to figure out what was going on the cause the loop.
Two word of caution, though: in some browsers, alert
and confirm
can sometimes make weird stuff happen in your code by screwing up the expected order of execution when, for instances, DOM events get triggered during an alert, as bobince describes in detail here: https://stackoverflow.com/a/2734311/1709587. They can potentially help you in infinite loops where console.log
is trickier to wield, but make sure you don't get tripped up by behavior changes introduced by putting alert
s into your code. Also note that reflows happen when you trigger an alert, which can change the behavior of code involving CSS transitions.
Also, I'm really not sure why you're not getting any output in the console from the sample code you've given. When I paste it into a jsFiddle and try it on Chrome, and then load the fiddle with the console open, the page is slow and unkillable and the whole browser a little buggered, but I do see things being logged (admittedly only every thousand lines or so). In Firefox, nothing happens for 5 seconds or so, but then I get an 'unresponsive page' popup and the option to kill the running script - after which I can see everything that was logged in the console. Perhaps trying a different browser (or the latest version of whatever browser you use) will make life easier for you without needing to deploy any tricks?