According to jQuery document on .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.
Could someone please expand on this? When is it more appropriate to use .delay()
, and when is it better to use .setTimeout()
?
version added: 1.4.An integer indicating the number of milliseconds to delay execution of the next item in the queue. queueName. Type: String. A string containing the name of the queue.
setTimeout allows us to run a function once after the interval of time. setInterval allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.
setTimeout() simply omits the window. , which is implied. The effect they have is exactly the same. It's a choice of coding style and preference. For JavaScript that does not run in a browser, the window object is not defined, so window.
Example 1: Display a Text Once After 3 Second Note: The setTimeout() method is useful when you want to execute a block of once after some time. For example, showing a message to a user after the specified time.
I think what you posted explains itself really.
Use .delay()
for jQuery effects including animations.
setTimeout()
is best used for everything else. For example when you need to trigger an event at a certain elapsed time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With