Skip to main content

Bug Tracker

Side navigation

#12025 closed feature (invalid)

Opened July 05, 2012 09:09PM UTC

Closed July 06, 2012 01:14AM UTC

Last modified July 06, 2012 01:20AM UTC

(Not Really) New jQuery Static Method: wait

Reported by: anonymous Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.2
Keywords: Cc:
Blocked by: Blocking:
Description

Ever since deferred was refactored out two years ago, every single article/demo/talk/etc. I've ever seen about how to use it has involved a mythical "wait" function. This function would replace window.setTimeout, eg.:

// Instead of:
window.setTimeout(doSomething, 50);

// Deferreds allow:
$.wait(50).then(doSomething);

It can also be found here:

http://docs.jquery.com/Cookbook/wait

Given that:

1) setTimeout is core part of JS (and thus is a problem shared by all JS devs), and

2) this is a "one-line" method (or at least as much as any jQuery method can be one-line), and

3) everyone and their brother, including the cookbook, is telling me to write my own plug-in for it, and

4) everyone and their brother (and the cookbook) are all telling me to write the same exact function

... wouldn't it just make sense to add a "wait" method to jQuery proper?

Attachments (0)
Change History (4)

Changed July 05, 2012 09:37PM UTC by anonymous comment:1

P.S. I realize that the implementation in the cookbooks isn't really one line ... but if you remove the extra "fx" option and delete some whitespace it's basically a one-liner ;-)

Changed July 05, 2012 09:39PM UTC by scottgonzalez comment:2

The code you link to is $.fn.wait() not $.wait() and does not do what you want. I'm not even sure why it exists instead of just using .delay().

Changed July 06, 2012 01:14AM UTC by dmethvin comment:3

resolution: → invalid
status: newclosed

All the code at docs.jquery.com is really old and will be either moved to a better place soon or thrown away.

That example reminds me of code like $(window).attr("location") to get or set the current URL instead of window.location--it's bad. There is already a perfectly good JavaScript method to schedule function execution, and to jQuery it up with $.wait(50).then(doSomething); doesn't add anything but overhead. There's no failure case for example, and no arguments passed to the function.

Changed July 06, 2012 01:20AM UTC by jaubourg comment:4

While I don't think this should be in core, I think equating such a functionality to setTimeout is a bit misleading.

Take the following code for instance:

$.when( somethingAsynchronous, $.wait( 2000 ) ).done(function() {
    // This will not be called before at least 2 seconds elapsed
});

As a construct this wait functionality can be very useful. It should be in a plugin though ;)