Skip to content

Instantly share code, notes, and snippets.

@uhtred
Last active August 29, 2015 13:57

Revisions

  1. uhtred revised this gist Mar 25, 2014. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions waitFor.js
    Original file line number Diff line number Diff line change
    @@ -4,13 +4,13 @@
    function waitFor ( options ) {
    options = $.extend( {}, {
    callback: function(){},
    condition: true,
    condition: function(){ return true },
    interval: 100
    }, options);

    options.intervalId = setInterval(function() {

    if( options.condition ) {
    if( options.condition() ) {
    clearInterval( options.intervalId );

    options.callback();
    @@ -21,7 +21,9 @@ function waitFor ( options ) {

    @Sample
    waitFor({
    condition: $( '#id' ).length,
    condition: function(){
    return $( '#id' ).length
    },
    callback: function(){
    $( '#id' ).text( 'test' );
    },
  2. uhtred created this gist Mar 24, 2014.
    31 changes: 31 additions & 0 deletions waitFor.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    @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: true,
    interval: 100
    }, options);

    options.intervalId = setInterval(function() {

    if( options.condition ) {
    clearInterval( options.intervalId );

    options.callback();
    }

    }, options.interval );
    }

    @Sample
    waitFor({
    condition: $( '#id' ).length,
    callback: function(){
    $( '#id' ).text( 'test' );
    },
    interval: 200
    });

    Suggestions??