我认为根据我的理解,回调函数被放置在回调队列中,只有在调用堆栈为空时才会执行。因此,在下面的代码中,为什么事件侦听器的回调函数在单击按钮时执行,而 console.log(index)
正在运行?应该在调用堆栈中存在所有 console.log()
函数的执行之后才更改背景颜色吗?
<button>Click me</button>
<script>
for (let index = 0; index < 100000; index++) {
console.log(index)
}
document.querySelector('button').addEventListener('click',()=>{
document.querySelector('body').style.backgroundColor = 'red'
})
</script>