Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
70
rated 0 times [  77] [ 7]  / answers: 1 / hits: 95365  / 13 Years ago, tue, november 15, 2011, 12:00:00

If I have a function which I would like my js code to run it immediately but after the run, wait for 2 seconds. How to achieve this logic?



(Note: It is just the inverse logic compare with setTimeout(), since setTimeout() first wait a certain amount of time then execute the function.)


More From » jquery

 Answers
26

Just put your code inside an anonymous function passed to setTimeout.



e.g.



functionToRunFirst();
setTimeout(function() {
// rest of code here
}, 2000);

[#89116] Monday, November 14, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janjadonb

Total Points: 4
Total Questions: 114
Total Answers: 118

Location: Mali
Member since Fri, Dec 3, 2021
3 Years ago
janjadonb questions
;