Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exclude setTimeout or setInterval type statements when debugging with "Break on Next"?

Tags:

javascript

I'm currently working with a large pre-existing codebase that may have one or more setInterval timers running all the time, from various plug-ins or libraries. This basically makes it impossible to try to use Break on Next to debug what happens when I click on an element.

Problem: As soon as I click Break on Next, the browser debugger (tried with Firebug and Chrome) stops in one of the setInterval functions before I have a chance to interact with the page to really debug the event that I want.

Specific problem: I have checkboxes that stay unchecked when unchecked, no matter how many times I click on them. I've removed the ID and class names as well to no avail and appear to have no event handlers attached.

Note: not using jQuery

like image 220
Michael Butler Avatar asked Jan 28 '13 20:01

Michael Butler


People also ask

How will you cancel the timeouts that are set with the setInterval?

To cancel the execution, we should call clearTimeout/clearInterval with the value returned by setTimeout/setInterval .

Why you should not use setInterval?

In case of time intensive synchronous operations, setTimeInterval may break the rhythm. Also, if any error occurs in setInterval code block, it will not stop execution but keeps on running faulty code. Not to mention they need a clearInterval function to stop it.

Should I use setInterval or setTimeout?

Usage notes. The setInterval() function is commonly used to set a delay for functions that are executed again and again, such as animations. You can cancel the interval using clearInterval() . If you wish to have your function called once after the specified delay, use setTimeout() .

Is setInterval deprecated?

We all know that passing a string to setTimeout (or setInterval ) is evil, because it is run in the global scope, has performance issues, is potentially insecure if you're injecting any parameters, etc. So doing this is definitely deprecated: setTimeout('doSomething(someVar)', 10000);


1 Answers

This may brake other things, but what if you try to monkey-patch-out those calls like this:

window.setInterval = function() { console.log("setInterval", arguments); };
window.setTimeout = function() { console.log("setTimeout", arguments); };

If you find that some of timeouts/intervals are actually needed to reproduce your problem, you could try letting them through. Than the code could be something like:

window.oldSetTimeout = window.setTimeout;
window.setTimeout = function() { 
    if (arguments[0] == "code you want to allow") {
         oldSetTimeout.apply(null, arguments);
    } else {
         console.log("setTimeout", arguments); 
    }
};

Note: I wouldn't be suprised it monkey-patching setTimeout does not work cross-browser, but it works on FF 18.0

like image 93
Marek Kowalski Avatar answered Oct 01 '22 22:10

Marek Kowalski