zhengwei1949 / myblog

个人博客
10 stars 6 forks source link

UI渲染是异步的代码说明 #151

Open zhengwei1949 opened 5 years ago

zhengwei1949 commented 5 years ago
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    #box{
      width: 100px;
      height: 100px;
      background-color: blue;
    }
  </style>
</head>
<body>
  <div id="box"></div>
  <script>
    setTimeout(function() {
      var box = document.getElementById('box');
      box.style.backgroundColor = 'red';

      var start = new Date;
      while (new Date - start < 2000) {
        // wait 1 second...
      };
    }, 0);
  </script>
</body>
</html>