Skip to content

Instantly share code, notes, and snippets.

@Sinetheta
Created November 29, 2012 22:35
Show Gist options
  • Save Sinetheta/4172367 to your computer and use it in GitHub Desktop.
Save Sinetheta/4172367 to your computer and use it in GitHub Desktop.
$.whenReady wait for arbitrary target to be ready
$.whenReady = function(target, delay) {
var dfd = $.Deferred();
var look = window.setInterval(function() {
var $target = $(target);
if($target.length) {
dfd.resolve($target);
window.clearInterval(look);
}
}, delay || 10);
return dfd;
};
$.whenReady('#slowpoke').done(function($target) {
console.log($target.length);
});
@Sinetheta
Copy link
Author

We use this in SharePoint instead of _spBodyOnLoadFunctionNames. Why? Because, like everything else in SP, the native solution is unreliable and has lead to broken code and crushed dreams.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment