Git Product home page Git Product logo

dowhen's Introduction

jQuery.doWhen( testFn, callbackFn, config )

doWhen is a jQuery plugin to solve all your wait-until-ready problems, wash your dishes for you, and blow dry your cat when it falls in the tub. All that for just 262 bytes, minified!

Have you ever called a function before all the things it depends on are ready? The dependencies might be an html node, a plugin, a dynamically generated script finished loading, an image finished loading, a server state checked via ajax... the possibilities are endless. If you've been coding Javascript as long as I have, you've probably created countless bespoke solutions to fit each case.

No more. Let's say you need to do something to an element once it is created:

jQuery.doWhen(function(){
  return !!document.getElementById('myelement');
}, function(){
  document.getElementById('myelement').innerHTML = "I'm loaded"!
});

The first function is a test. When it returns a truthy value, the second function is run. It's as simple as that.

If you need the second function to run in a specific context (where the "this" keyword points to), use the config parameter:

jQuery.doWhen(..., ..., {
  context: myObject
}

You can also pass it data:

jQuery.doWhen(..., ..., {
  data: {
    blix: 'blarg'
  }
});

And, lastly, if you want to use a different interval than the default of 100ms between tests (which you'd definitely do for any test involving ajax):

jQuery.doWhen(..., ..., {
  interval: 60 * 1000 //once every minute
}

Note that it would be very easy to adapt this to remove the jQuery dependency. The only thing it's needed for is a shallow "extend" and a namespace to put the object.

dowhen's People

Contributors

bitbonsai avatar

Stargazers

 avatar

Watchers

 avatar  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.