Use a delayed trigger method

I would use a trigger method with a delay before execution, I try as follows:

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

but the code runs instantly.

Did any of you help?

Many thanks

+6
source share
3 answers

jQuery doc says :

The .delay () method is best delayed between jQuery effects in the queue. Since it is limited - it, for example, does not offer a way to cancel the delay .delay () is not a replacement for the setTimeout built-in function for JavaScript, which may be more suitable for certain use cases.

So, I would rewrite this as

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

From jQuerys delay documentation:

The .delay () method is best delayed between jQuery effects in the queue. Since it is limited - it, for example, does not offer a way to cancel the delay .delay () is not a replacement for the setTimeout built-in function for JavaScript, which may be more suitable for certain use cases.

In other words, you should use setTimeout (), namely:

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

Try:

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

Source: https://habr.com/ru/post/904976/


All Articles