0

I'm working on a javascript UI which assumes that there are a certain grid elements that need to be looked-at/examined by the user and then a confirmation shall pop up to query user's intentions and carry them out accordingly.

The code goes something like this:

elements.forEach(x => {
    x.style.backgroundColor = "red";
    x.scrollIntoView();
    let r = confirm("Do you wish to alter element?");
    //function code
});

Once the code is executed, window.confirm doesn't wait for the element style changes to complete and for the element itself to be scrolled into view.

Is there a workaround for this behavior of window.confirm?

Noodles
  • 11
  • 1

1 Answers1

0

You can try to force the confirm() to execute in the next event loop with a timeout

elements.forEach(x => {
    x.style.backgroundColor = "red";
    x.scrollIntoView();
    setTimeout (function () {
        let r = confirm("Do you wish to alter element?");
        //function code
    }, 0)
});
Maxi Diaz
  • 282
  • 1
  • 10