Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

关于setTimeout跟setInterval以及event loop一个题目

$
0
0
console.log('script start')

const interval = setInterval(function(){
        console.log('setInterval')
}, 0)

setTimeout(function(){
    console.log('setTimeout 1')
Promise.resolve().then(function(){
    console.log('promise 3')
}).then(function() {
    console.log('promise 4')
}).then(function() {
    setTimeout(function(){
    console.log('setTimeout 2')
    Promise.resolve().then(function() {
        console.log('promise 5')
}).then(function() {
        console.log('promise 6')
}).then(function() {
        clearInterval(interval)
    })
}, 0)
})
}, 0)

Promise.resolve().then(function() {
    console.log('promise 1')
}).then(function() {
    console.log('promise 2')
})

在试验的时候会生成untitled1.png,但是我在一个模拟器中会出现当输出setTimeout 1后会一直输出setInterval,而不会再往后面执行,求这个是什么原因,个人更倾向第二种结果


Viewing all articles
Browse latest Browse all 14821

Trending Articles