Last active
August 29, 2015 13:57
-
-
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.
This file contains hidden or 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
@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?? |
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
Superagent's end() method's does that on Ajax Calls. Check it out.
http://visionmedia.github.io/superagent/