Code Monkey home page Code Monkey logo

wait-for-it's Introduction

Wait For It

Return a promise that waits until a selector matches an element. It will resolve when the element exists in the DOM or reject if the timeout is reached.

var promise = $('.future-class').waitForIt()

promise.then(
    function($element) {
        console.log('resolved: ', $element)
    }, 
    function($element) {
        console.log('rejected: ', $element)
    }
)

Usage

// testing it out
$('.invalid-class').remove()

$('.invalid-class')
    .waitForIt({timeout: 1000, interval: 10})
    .then(x => console.log('resolved: ', x), x => console.log('rejected: ', x))

setTimeout(function() {
    $('body').append($('<div />').addClass('invalid-class'))
    console.log('appended')
}, 1000)

Options

  • interval: How often to run the resolveCheck (in milliseconds). The default is every 100 milliseconds.
  • timeout: Reject the promise after checking for this long (in milliseconds) default is 1 minute (60,000 milliseconds). To run until resolved set the timeout to -1.
  • minLength: The default check resolves when this many elements match the selector
  • selector: This selector is used to match against each interval. The default uses the deprecated .selector from the jQuery object.
  • resolveCheck: this function is run each interval, if it returns a truthy value the promise is resolved.
  • rejectCheck: this function is run each interval, if it returns a truthy value the promise is rejected.

The resolveCheck and rejectCheck functions have the same arguments

  • the jQuery object
  • the options default Options extended with those provided
  • the ammount of time the check has run for so far (in milliseconds)

Default Options

var defaultOptions = {
    interval: 100,
    timeout: 6e4,
    selector: $(this).selector,
    minLength: 1,
    resolveCheck: function($ele, settings, runTime) {
      return $ele.length >= settings.minLength
    },
    rejectCheck: function($ele, settings, runTime) {
      return $ele.length < settings.minLength && settings.timeout <= runTime
    },
}

wait-for-it's People

Contributors

wdietz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.