Home > Blockchain >  document.body.style.backgroundColor getting executed after next line
document.body.style.backgroundColor getting executed after next line

Time:11-21

my teacher is asking us a question and i couldn't find an answer: when pressing a button on page, run those two lines:

document.body.style.backgroundColor = "Green";
alert("Done");

why is it first alerting "Done", and then changing the background color? thanks.

CodePudding user response:

This is the alert behavior. It is triggered before page loaded. To prevent this behavior, try alert the message asynchronnaly. Like this:

document.body.style.backgroundColor="green";
setTimeout(alert, 50, "Done");

CodePudding user response:

beacuse the code is already done when u press the putton if u need to make it run before changing color u can use setTimeout(code, [delay]); delay for num of mill seconds thah u need to wait before executing

  • Related