Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.delay() and .setTimeout()

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()?

like image 807
Randomblue Avatar asked Sep 13 '11 20:09

Randomblue


People also ask

What is delay () in JavaScript?

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.

What is difference between setTimeout and setInterval?

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.

What is the difference between setTimeout and window setTimeout?

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.

Why setTimeout function is used?

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.


1 Answers

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.

like image 153
adamjmarkham Avatar answered Oct 26 '22 16:10

adamjmarkham