I was playing around with timers the other day, trying to see how different browsers handled different edge cases, when I stumbled across a fascinatingly bizarre quirk in Internet Explorer. I wouldn’t call it a bug, per se, since that would imply that there was an excepted result, but there is not. The code:
Now, before you view the demo (view in IE only!) try to guess what the above code does.
Since -1 isn’t a valid interval my guess was that it would, either (based upon Internet Explorer’s current setInterval behavior):
- Execute immediately, like a normal function, but then never fire again.
- or not execute at all.
What happened is positively bizarre: The callback function will be executed every time the user left clicks the mouse, anywhere in the document.
I find this to be absolutely hilarious. I’m struggling to think of how this could, possibly, be used in any remotely feasible way. Some random ideas:
- A global click handler that can’t be removed and doesn’t leak memory (presumably since no DOM elements involved?)
- Some sort of vector for XSS attacks. I don’t know what the situation would be where you have access to setInterval but not the DOM, so I’m not sure what credence this has.
- A way to initiate a fake event capturing click event (before the actual bubbling event occurs).
I don’t know – I’m not sure if any of them are terribly useful but I find it to be amusing nonetheless. Thoughts on how we can have fun with this?