Created
November 11, 2012 23:35
-
-
Save Swader/4056723 to your computer and use it in GitHub Desktop.
Paul Irish's Javascript Tricks from jQuery 1.4.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Paul Irish dissects jQuery's source code and explains some sexy practices and pitfalls. Here's a summary. | |
// This pattern is a self-executing anonymous function. It helps minification, faster scope access to document and eliminates the asshole effect where someone writes undefined = true in your JS | |
(function(window, document, undefined){ | |
/*... code here ...*/ | |
})(this, document); | |
// In short, the self calling pattern is as follows: | |
(function f() {})(); | |
// or with arguments | |
(function f(arg1, arg2){})(arg1Value, arg2Value); | |
// doStuff might last more than 100ms, so we're using setTimeout instead of setInterval. This method allows the calls to still have 100ms between calls, even if the calls last more than 100ms, and prevents them stacking if one fails to execute within its designated 100ms. | |
(function loop(){ | |
doStuff(); | |
setTimeout(loop, 100); | |
})(); | |
// The above method also works great for asnyc recursion - preventing us from calling a resource before the previous one fully loaded. | |
(function loop(){ | |
doStuff(); | |
$("#update").load("some.php", function(){loop();}, "json"); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment