Why does alert appear before background changes?

前端 未结 3 1585
长发绾君心
长发绾君心 2020-12-11 17:14

So, I\'ve been trying stuff lately and got this piece of code in my script:

document.body.bgColor = \"red\";
alert(\"hello\");

But in Chrom

3条回答
  •  抹茶落季
    2020-12-11 17:43

    The simplest workaround will be like:

    document.body.bgColor = "red";
    setTimeout(function() {
            window.alert('Hello There!');
        },  10);
    

    The timeout value "9" is the minimum in my case, if i use <9, alert appears first.

提交回复
热议问题