Stop page execution like the alert() function

Viewed 80197

When I write alert('Hello'), the page execution stops and waits for approval to continue.

I have a div setup to display as a fake alert, using HTML - this div has an 'OK' button.

I want the page to stop its execution (just like alert does) until the user click 'OK'.

Is it possible ?

8 Answers

If you want to wait for the dialog box to close, the "open" property indicates whether the dialog box is open or closed. When it's done, resolve the promise. For synchronization, add a timer to periodically test this property.

    let d = document.querySelector('dialog')
    d.showModal()
    await new Promise((resolve, reject) => {
        let timer = setInterval(_ => {
            if (!d.open) {
                resolve()
                clearInterval(timer)
            }
        }, 500)
    })
Related