1

If I have a function

function hello() {
    alert("hello world!");
}

and I want to call it, I just do hello(), but if I want to do a delay? Should I do:

$.delay(3000).hello();

Would this work?:

$.fn.hello = function() {
    alert("hello world!");
}
$.delay(...).hello()
5
  • Why can't you use setTimeout?
    – Igor
    Jun 13, 2014 at 21:10
  • @IgorShults just a different way of using it, nothing but that.
    – user3144310
    Jun 13, 2014 at 21:11
  • I believe .delay() is for use only with animations. Such as: slideup().delay().slidedown() Jun 13, 2014 at 21:12
  • 1
    @JuanRocamonde delay won't work, it's just for jQuery effects/animations. setTimeout is really what would typically be used here.
    – Igor
    Jun 13, 2014 at 21:12
  • @IgorSgults would the update work?
    – user3144310
    Jun 13, 2014 at 21:16

2 Answers 2

2

Is this homework, or some code golf problem? Because setTimeout is really the ideal function in this case, and delay won't work because it's just for jQuery effects/animations.

I suppose you could use setInterval and clear after the first invocation. Something like:

var interval;

function hello() {
    clearInterval(interval);
    alert("hello world!");
}

interval = setInterval(hello, 3000);

If you can't alter the hello() function or use it elsewhere, you can create a wrapper function for the setInterval call, and use that instead.

2
  • I like your answer, which demostrates that things can be reached many ways. The point was about doing it different, but I wasn't waiting for something like this.
    – user3144310
    Jun 13, 2014 at 21:21
  • @Igor I think it's worth emphasising the point that $.delay() only affects the jQuery animation queue. Otherwise, perfect answer
    – Bojangles
    Jun 13, 2014 at 21:22
2

You can always just add to the queue

$.fn.hello = function() {
    alert("hello world!");
    return this;
}

$(document).delay(3000).queue(function() {
    $(this).hello().dequeue();
});

FIDDLE

The reason it only works on animations, is because those are added to the FX queue automagically, but anything can be queued and dequeued in jQuery, you can even create your own custom queues that will work with delay()

For a custom queue you can do something like this

$({}).delay(3000, 'custom').queue('custom', function( next ) {
    $(this).hello();
}).dequeue('custom');

FIDDLE

This is a little different, it's showing us that we can use an empty jQuery object as the holder and not just an element, and the second argument in delay() is used to pass in the name of the queue, which we then attach and dequeue a little differently.

We're not really using the next argument as there is nothing more in the queue to call, but it's an argument that you call to go the next step, as in you'd call next() inside the function to proceed.

2
  • I think if you're going to queue something custom, you might want to show targeting the custom queue. Using the default .delay() isn't good because animations/effects from anywhere affect this
    – Ian
    Jun 13, 2014 at 21:38
  • 1
    @Ian - Agreed, added an example that shows a custom queue and a few other things as well.
    – adeneo
    Jun 13, 2014 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.