Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wait() or sleep() function in jquery?

Tags:

jquery

I want to add a class, wait 2 seconds and add another class.

.addClass("load").wait(2sec).addClass("done"); 

Is there any way?

like image 311
Steven Moss Avatar asked Apr 19 '11 21:04

Steven Moss


People also ask

What is the use of delay () method in jQuery?

jQuery delay() Method The delay() method sets a timer to delay the execution of the next item in the queue.

How would you wait or sleep in the code?

Sleep is a function that is present in most programming languages and is used to delay the execution of code for a specified time. JavaScript does not have this function, so we use the setTimeout() function to perform the same task.

How do you wait for 5 seconds in JavaScript?

Usage: const yourFunction = async () => { await delay(5000); console.


1 Answers

setTimeout will execute some code after a delay of some period of time (measured in milliseconds). However, an important note: because of the nature of javascript, the rest of the code continues to run after the timer is setup:

$('#someid').addClass("load");  setTimeout(function(){   $('#someid').addClass("done"); }, 2000);  // Any code here will execute immediately after the 'load' class is added to the element. 
like image 102
ctcherry Avatar answered Sep 30 '22 18:09

ctcherry