Gildas Ross
Gildas Ross

Reputation: 3892

Use trigger method with delay

I would use the trigger method with a delay before the execution, I try this way:

$('#open-contact').delay(3000).trigger('click');

but the code runs instantly.

Do any of you could help me?

thank you very much

Upvotes: 7

Views: 20595

Answers (3)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Try:


$('#open-contact').delay(3000).queue(function() {
    $(this).trigger('click');
});


Upvotes: 5

lfxgroove
lfxgroove

Reputation: 3908

From jQuerys documentation about delay:

The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.

In other words you should use setTimeout() instead, ie:

setTimeout(function () { $('#open-contact').trigger('click'); }, 3000);

Upvotes: 6

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230346

jQuery doc says:

The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.

So, I would rewrite this as

setTimeout(function() {
    $('#open-contact').trigger('click');
}, 3000);

Upvotes: 17

Related Questions