Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

javascript - How to wait for a period of time after a function run

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.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

e.g.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...