Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Twitter redefine window.setTimeout and window.setInterval?

I was studying the Twitter source code, and I came across the following snippet:

   window.setTimeout=window.setTimeout;window.setInterval=window.setInterval;

Why does Twitter redefine these functions?

Edit: To see the code, go to any twitter user page, open the source of the page, and you will see that snippet in the second block of javascript.

like image 444
jonathancardoso Avatar asked Nov 08 '11 00:11

jonathancardoso


1 Answers

This is a technique to replace setTimeout and setInterval functions globally in a cross-browser fashion.

window.setTimeout, when used as an lvalue (on the left side of the assignment), does not walk the prototype chain, but on the right side, it does. So this will always pull a property out of the prototype chain and put it right on the object.

See http://www.adequatelygood.com/2011/4/Replacing-setTimeout-Globally.

like image 123
ayanami Avatar answered Sep 21 '22 13:09

ayanami