微任务,宏任务

117 阅读1分钟

微任务

        Promise , async , await

宏任务

       setTimeoutsetIntervalDOM事件 ,AJAX请求
  1. JS是单线程执行
  2. 顺序 : 同步代码 ----> 微任务 ----> DOM渲染 ----> 宏任务
  3. 是一个存储函数调用的栈结构,遵循先进后出的原则
  4. Event Loop 循环机制
console.log(1);

setTimeout(() => {

console.log(2)

}, 0);

Promise.resolve().then(() => {

console.log(3)

})

console.log(4);
//Dom
const $content = $('<p>内容</p>');

$('#box').append($content);

console.log(1);

setTimeout(()=>{

    console.log(2);

},0);

Promise.resolve().then(()=>{

    console.log(3);

    alert('promise then ');

});

console.log(4);