简体   繁体   English

javascript-setTimeout()与setInterval()

[英]javascript - setTimeout() vs setInterval()

What are the minor and major differences between setTimeout() and setInterval() ? setTimeout()setInterval()之间的次要和主要区别是什么?

I searched the internet but it made me confused! 我搜索了互联网,但是这让我感到困惑! What's the difference between those? 两者之间有什么区别?

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? setTimeout或setInterval?

'setInterval' vs 'setTimeout' 'setInterval'与'setTimeout'

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 主要区别在于setTimeout将在给定延迟后仅执行一次代码,而setInterval将始终执行代码,每次调用之间都存在延迟

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

From Javascript timers MDN Javascript计时器MDN

setTimeout () setTimeout ()

Calls a function or executes a code snippet after specified delay. 在指定的延迟后调用函数或执行代码段。

setInterval () setInterval ()

Calls a function or executes a code snippet repeatedly, with a fixed time delay between each call to that function. 重复调用一个函数或执行代码段,每次调用该函数之间有固定的时间延迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM