javascript - 怎么理解js的这个看起来同步,但是却是异步的demo
黄舟
黄舟 2017-04-10 14:52:49
0
2
431

http://jsfiddle.net/hidoos/SNBYV/259/embedded

html<style>
    #box {
        width: 100px;
        height: 100px;
        background-color: blue;
    }
</style>
<p id="box"></p>
jsvar box = document.getElementById('box');
box.style.backgroundColor = 'red';

var start = new Date;
while (new Date - start < 3000) {
    // wait 3 second...
};

box的初始颜色是blue,我以为按照顺序执行的话,不用等待3s就能直接修改box的颜色为red,而实际的情况则是要等待了3s才修改box的颜色。不是很明白为什么会这样。

为什么不直接执行dom操作,非要等while执行这个3s,才去执行dom操作呢?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!