浏览器事件循环和我的头发

50次阅读

共计 621 个字符,预计需要花费 2 分钟才能阅读完成。

        btn.addEventListener("click", event => {setTimeout(() => {console.log("S1")}, 0)
            Promise.resolve().then(() => console.log("M1"))
            console.log("L1")
        })
        btn.addEventListener("click", event => {setTimeout(() => {console.log("S2")}, 0)
            Promise.resolve().then(() => console.log("M2"))
            console.log("L2")
        })

点击 btn
输出:L1、M1、L2、M2、S1、S2
大家都很聪明,应该都理解!

        btn.addEventListener("click", event => {setTimeout(() => {console.log("S1")}, 0)
            Promise.resolve().then(() => console.log("M1"))
            console.log("L1")
        })
        btn.addEventListener("click", event => {setTimeout(() => {console.log("S2")}, 0)
            Promise.resolve().then(() => console.log("M2"))
            console.log("L2")
        })
        btn.click()

输出:L1、L2、M1、M2、S1、S2

正文完
 0