新鲜面试题

63 阅读1分钟

给你下面的代码,请说说页面出现和控制台输出的顺序(包括dom解析渲染和JS解析执行顺序)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script src="./a.js" defer></script>
  <script src="./b.js" async></script>
  <div>123</div>
  <script>
    let c = 0;
    setTimeout(() => console.log('timeout log'), 0);
    while(c < 20000) {
      c += 0.0001;
    }
    console.log('log c 1');
  </script>
  <div>456</div>
  <script>
    c = 0;
    while(c < 20000) {
      c += 0.0001;
    }
    console.log('log c 2');
  </script>
</body>
</html>

a.js

var a = 0;
while (a < 20000) {
  a += 0.0001;
}
console.log('log a');

b.js

let b = 0;
while (b < 20000) {
  b += 0.0001;
}
console.log('log b');

好家伙,不理解这代码的解析执行顺序了,有没有大佬来解释解释