Bug Tracker

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#12025 closed feature (invalid)

(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?

Change History (4)

comment:1 Changed 7 years ago by anonymous

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 ;-)

comment:2 Changed 7 years ago by scottgonzalez

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().

comment:3 Changed 7 years ago by dmethvin

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.

comment:4 Changed 7 years ago by jaubourg

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 ;)

Note: See TracTickets for help on using tickets.