简体   繁体   English

setTimeout() 和 setInterval() 函数是异步的吗?

[英]Are setTimeout() and setInterval() functions async?

I'm new for front end.我是前端新手。 In Javascript, are setTimeout() and setInterval() functions async?在 Javascript 中,setTimeout() 和 setInterval() 函数是异步的吗? I read some tutorial that use these two functions to imitate async, but I don't know if they really are.我阅读了一些使用这两个函数来模拟异步的教程,但我不知道它们是否真的如此。

They are asynchronous and run in the JavaScript event loop .它们是异步的并在 JavaScript 事件循环中运行。 However they are not async , which is a syntactic sugar around functions that work with Promises .但是它们不是async ,它是与Promises一起使用的函数的语法糖。

We can provide a concrete example of how setTimeout is used to easily leverage async and await .我们可以提供一个具体示例,说明如何使用setTimeout轻松利用asyncawait Click run snippet to verify the results in your own browser -单击运行片段以在您自己的浏览器中验证结果 -

 const sleep = ms => new Promise(r => setTimeout(r, ms)) // <- Promise around setTimeout async function main() { console.log("powering on. please wait...") await sleep(1000) console.log("initializing system resources...") await sleep(1500) console.log("133 MHZ CPU deteced") await sleep(1200) console.log("64 KB RAM available") await sleep(2000) console.log("120 MB hard drive deteced") await sleep(500) console.log("registering PCI devices...") await sleep(1000) console.log("starting MS-DOS...") return "ready" } main().then(console.log, console.error)

powering on. please wait ...
initializing system resources...
133 MHZ CPU deteced
64 KB RAM available
120 MB hard drive deteced
registering PCI devices...
starting MS-DOS...
ready

Both take a function as a parameter and schedule that function to be executed later.两者都将 function 作为参数并安排 function 稍后执行。 So, one could say they are "async" in a sense.因此,可以说它们在某种意义上是“异步的”。 However, in JavaScript the "async" word has a very specific meaning, since async is a keyword used to declare a function, and in this sense they are not async, since they do not return a Promise.然而,在 JavaScript 中,“async”这个词有一个非常具体的含义,因为async是一个用于声明 function 的关键字,从这个意义上说,它们不是异步的,因为它们不返回 ZA5A3F0F2587A44898FE.AAC

quick answer javascript is synchronous but some callbacks act async.快速回答 javascript 是同步的,但一些回调是异步的。

the setTimeout() function will be triggered in the stack, then continue on with what comes after even though it has not finished its timer. setTimeout() function 将在堆栈中触发,然后继续执行后面的操作,即使它还没有完成它的计时器。

Javascript is naturally synchronous, but some callbacks are async like setTimeout and setInterval, now if you want to wait for something, consider using a promise instead new Promise(() => console.log() . - I gave bad information, fixed it now Javascript 自然是同步的,但是一些回调是异步的,比如 setTimeout 和 setInterval,现在如果你想等待什么,考虑使用 promise 代替new Promise(() => console.log() 。 - 我提供了错误的信息,修复了它现在

If you write a simple block of code like so, you'll notice that non timeout will print then interval then timeout even though it's laid in the opposite order.如果您像这样编写一个简单的代码块,您会注意到non timeout将打印然后是interval然后timeout ,即使它以相反的顺序放置。

setTimeout(() => console.log('timeout'), 3000)
setInterval(() => console.log('interval'), 2000)
console.log('non timeout')

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

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