Is innerHTML asynchronous?

后端 未结 3 471
甜味超标
甜味超标 2020-11-29 20:12

I hope I won\'t make a fool of myself but I\'m trying to understand what is happening in those two lines of code:

document.body.innerHTML = \'something\';
al         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 20:40

    Yes, it is synchronous, because this works (go ahead, type it in your console):

    document.body.innerHTML = 'text';
    alert(document.body.innerHTML);// you will see a 'text' alert
    

    The reason you see the alert before you see the page changing is that the browser rendering takes more time and isn't as fast as your javascript executing line by line.

提交回复
热议问题