事件循环 event loop

2020-08-24  本文已影响0人  育儿与心理

进程 线程

浏览器的进程

浏览器的渲染流程

浏览器内核

宏任务,微任务

loop.jpg
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <!-- script是一个宏任务 -->
  <script>
    document.body.style.background = 'red';
    console.log(1);
    // 在 setTimeout 执行前 会触发一次渲染
    Promise.resolve().then(()=>{
      console.log(2);
      document.body.style.background = 'yellow';
    });
    console.log(3);
  </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button id="button">点我啊</button>
  <script>
    button.addEventListener('click', ()=>{
        console.log('listener1');
        Promise.resolve().then(()=>console.log('micro task1'));
    });
    button.addEventListener('click', ()=>{
        console.log('listener2');
        Promise.resolve().then(()=>console.log('micro task2'));
    });
    // 点击按钮之后的顺序
    // listener1
    // micro task1
    // listener2
    // micro task2

    // 一个个的函数来取 每次执行完 会先处理当前定义的微任务
    button.click(); // click1() click2()
    // listener1 listener2 micro task1 micro task2
  </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    Promise.resolve().then(() => {
      console.log('Promise1')
      setTimeout(() => {
        console.log('setTimeout2');
      }, 0);
    });
    
    setTimeout(() => {
      console.log('setTimeout1');
      Promise.resolve().then(() => {
        console.log('Promise2');
      });
    }, 0);
    // Promise1 setTimeout1 Promise2 setTimeout2
  </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
  <script>
    console.log(1);
    async function async () {
      console.log(2);
      await console.log(3); // Promise.resolve(console.log(3)).then(console.log(4))
      console.log(4);
    }
    setTimeout(() => {
      console.log(5);
    }, 0);
    const promise = new Promise((resolve, reject) => {
      console.log(6); // new Promise 代码会立即执行
      resolve(7);
    })
    promise.then(res => { // 第一个微任务
      console.log(res)
    });
    async (); 
    console.log(8);
    // 1 6 2 3 8 7 4 5
  </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
  <script>
    console.log('script start');

    setTimeout(function() {
      console.log('setTimeout');
    }, 0);

    Promise.resolve().then(function() {
      console.log('promise1');
    }).then(function() {
      console.log('promise2');
    });

    console.log('script end');

    // script start
    // script end
    // promise1
    // promise2
    // setTimeout
  </script>
</body>
</html>
  // 1 7 6 8
  console.log('1');
  setTimeout(function() {
    console.log('2');
    process.nextTick(function() {
      console.log('3');
    });
    new Promise(function(resolve) {
      console.log('4');
      resolve();
    }).then(function() {
      console.log('5')
    });
  }, 0);
  process.nextTick(function() {
    console.log('6');
  });
  new Promise(function(resolve) {
    console.log('7');
    resolve();
  }).then(function() {
    console.log('8')
  });

  setTimeout(function() {
    console.log('9');
    process.nextTick(function() {
        console.log('10');
    });
    new Promise(function(resolve) {
        console.log('11');
        resolve();
    }).then(function() {
        console.log('12')
    });
  }, 0);
上一篇 下一篇

猜你喜欢

热点阅读