Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - await bootstrap modal close by user

I have a modal that shows a message before some other code runs after it.

I want to prevent the code until the user has pressed the OK button on it. I tried async/await like this below, which doesn't work, of course.

async function myMessage(){
    $('#id_my_modal').modal('show');
}

await myMessage();

I know I could use alert() but that doesn't work when screen is off while that code is executed. I am making a webRTC video call app. If there is incoming call I show a message which works when user is screen is on. But when screen is off and there is an incoming call alert() doesn't appear. Same with media permission dialog. It happens in FF, at least. But a modal appears even when the screen was off. That is why I want a modal.

Help would be much appreciated.

like image 215
Newbie Avatar asked Nov 16 '25 17:11

Newbie


1 Answers

Finally got it working by using promises.

async function myMessage(){
    $('#id_my_message_modal').modal('show');
    return new Promise(resolve => 
        $('#id_my_message_button').on('click', () => {
                $('#id_my_message_modal').modal('hide');
                resolve();
            }
        )
    );
}

then

await myMessage();
like image 159
Newbie Avatar answered Nov 18 '25 07:11

Newbie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!