GithubHelp home page GithubHelp logo

xportde / jquery.wait Goto Github PK

View Code? Open in Web Editor NEW

This project forked from madbook/jquery.wait

0.0 1.0 0.0 11 KB

inline window.setTimeout / deferred method chaining

License: MIT License

JavaScript 100.00%

jquery.wait's Introduction

jQuery.wait

inline window.setTimeout

jQuery.wait allows you to easily insert a delay into a chain of jQuery methods. This allows you to use timeouts without uglifying your code and without having to use a custom queue. You can also now delay execution inline by passing a promise to .wait().

How is this different than .delay()?

The built-in .delay() method provided by jQuery uses the built-in queue implementation. Queues in jQuery require one of two things:

  • function calls in the queue must be manually dequeued or
  • functions in the queue must support auto-dequeuing by calling the .dequeue() internally

This basically means that jQuery methods must opt-in to the queue. Most common jQuery methods do not do this, and thus ignore the queue completely. Here's an example. Let's say we need to add a class to the body, wait 5 seconds, then remove it. Without .wait(), that would look like this:

$('body').addClass('foo').delay(5000).removeClass('foo');

This may look like it would do the right thing, but what actually happens is that the delay call is completely ignored. Non-animation methods in jQuery don't support queueing out of the box, so they are executed immediatly.
jQuery.wait uses its own queueing implementation, allowing it to support any jQuery method that returns a jQuery object, including methods of other plugins.

Example 1 - Setting a Timeout Inline

Here is a trivial example of how .wait() can be used to pause execution using a timeout. I'll use the same example as above. Here's what it looks like normally:

 $('body').addClass('foo');
 
 window.setTimeout(function(){
   $('body').removeClass('foo');
 }, 5000);

Not exactly elegant. The .wait() method allows us to define this timeout in-line and keep things nice and neat.

$('body').addClass('foo').wait(5000).removeClass('foo');

Example 2 - Deferring Execution with a Promise

The .wait() method recently added support for creating a promise-based delay inline. To use the same example as above, without .wait() :

var deferred = $.Deferred();
var promise = deferred.promise();

$('body').addClass('foo')
promise.then(function () {
    $('body').removeClass('foo');
});

As with the first example, having to break up the method chain is a little ugly. With .wait(), we can keep it all in-line:

var deferred = $.Deferred();
var promise = deferred.promise();

$('body').addClass('foo').wait(promise).removeClass('foo');

Example 3 - One-time event binding

You can also pass an event name in as a string, and the remaining commands will execute the next time that event occurs. Example time:

Without wait

$('body').addClass('foo');
 
$('body').one('click', function () {
    $(this).removeClass('foo');
});

With wait

$('body').addClass('foo').wait('click').removeClass('foo');

Disclaimer - Using with CSS Transistions

If you are using .wait() to add/remove classes that controls css transitions, the duration of the wait needs to be slightly longer than the transition time. So, if in the example above the class foo added a 5 second transition of some sort, I would need to make the wait time longer. I recommend 100ms longer, though your needs may vary depending on the complexity of the animation.

If you are chaining jQuery transitions, it is better to use the built-in .delay() method, which has the same syntax but works with jQuery queues.

jquery.wait's People

Contributors

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