javascript - setTimeout() vs setInterval()

南笙酒味 提交于 2019-12-21 21:39:19

问题


What are the minor and major differences between setTimeout() and setInterval()?

I searched the internet but it made me confused! What's the difference between those?


回答1:


The main diffrence is

setInterval fires again and again in intervals, while setTimeout only fires once.

you can get more differnces in simple words in

setTimeout or setInterval?

'setInterval' vs 'setTimeout'




回答2:


From Javascript timers MDN

setTimeout()

Calls a function or executes a code snippet after specified delay.

setInterval()

Calls a function or executes a code snippet repeatedly, with a fixed time delay between each call to that function.




回答3:


tha major difference is that setTimeout will execute some code just once, after a given delay, while setInterval will execute a code always, with a delay between each call

e.g. try these on your console:

setTimeout(function() {
  console.log('Wait 3 seconds and I appear just once');
}, 3000);

and

setInterval(function() {
  console.log('Every 3 seconds I appear on your console');
}, 3000)


来源:https://stackoverflow.com/questions/17486823/javascript-settimeout-vs-setinterval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!