Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 606
Gildas Ross Avatar asked Jan 03 '12 11:01

Gildas Ross


2 Answers

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);
like image 96
Sergio Tulentsev Avatar answered Nov 02 '22 23:11

Sergio Tulentsev


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);
like image 40
lfxgroove Avatar answered Nov 02 '22 22:11

lfxgroove