Skip to content

Instantly share code, notes, and snippets.

@uhtred
Last active August 29, 2015 13:57
Show Gist options
  • Save uhtred/9748220 to your computer and use it in GitHub Desktop.
Save uhtred/9748220 to your computer and use it in GitHub Desktop.
@problem: When I have no control on some ajax or generated html and I need to wait for them. @Solution: Set an interval to check my conditions and them execute a callback.
@Problem: When I have no control on some ajax or generated html and I need to wait for them.
@Solution: Set an interval to check my conditions and them execute a callback.
function waitFor ( options ) {
options = $.extend( {}, {
callback: function(){},
condition: function(){ return true },
interval: 100
}, options);
options.intervalId = setInterval(function() {
if( options.condition() ) {
clearInterval( options.intervalId );
options.callback();
}
}, options.interval );
}
@Sample
waitFor({
condition: function(){
return $( '#id' ).length
},
callback: function(){
$( '#id' ).text( 'test' );
},
interval: 200
});
Suggestions??
@heltonvalentini
Copy link

Superagent's end() method's does that on Ajax Calls. Check it out.

http://visionmedia.github.io/superagent/

@uhtred
Copy link
Author

uhtred commented Mar 25, 2014

Cool! But i need just the ugly part. :)

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