Jacob Larsen Jacob Larsen - 1 year ago 72
Javascript Question

Basic JavaScript - How to make a certain lines of code run before prompts/prompt-containing functions?

function restart_game() {
document.getElementById("myBody").style.backgroundColor = "rgba(255,255,255,1)";
do_game(); //do_game() has a prompt within it that interrupts the .document line.

So let's say I want to run
(a function that calls a prompt) but they are called by the same function: How do I make this happen effectively?

I want them both to run but in the order that I decide, without the prompt within
happening before my style changes in the line before. Or is there just a better way to achieve what I'm attempting to do with different code?

Any advice would be highly welcome.

Answer Source

I would do this in order to make sure that do_game() runs after style changes:

var myBody = document.getElementById("myBody");

function restart_game() {
   myBody.style.backgroundColor = "rgba(255,255,255,1)";

myBody.addEventListener("DOMAttrModified", function(e){
   if (e.attrName === 'style')

Explanation: This way, restart_game() function will change style of "myBody" element. When that happens, eventListener attached to "myBody" element will detect style changes and execute do_game() so you'll have their execution aligned.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download