Bug Tracker

Modify

Ticket #12025 (closed feature: invalid)

Opened 12 months ago

Last modified 12 months ago

(Not Really) New jQuery Static Method: wait

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

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

comment:1 Changed 12 months 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 12 months ago by scott.gonzalez

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 12 months ago by dmethvin

  • Status changed from new to closed
  • Resolution set to invalid

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

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.