Is there a way I can do a sleep in JavaScript before it carries out another action?
Example:
var a = 1 + 3; // Sleep 3 seconds before the next action here. var b = a + 4;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
setTimeoutto achieve a similar effect:This doesn’t really ‘sleep’ JavaScript—it just executes the function passed to
setTimeoutafter a certain duration (specified in milliseconds). Although it is possible to write a sleep function for JavaScript, it’s best to usesetTimeoutif possible as it doesn’t freeze everything during the sleep period.