简体   繁体   中英

How can I repeatedly play a sound while a javascript alert message is up and stop it when the alert message is dismissed?

How can I repeatedly play a sound while a javascript alert message is up and stop it when the alert message is dismissed?

I already have an HTML5 sound element on the page.

I've tried:

while(alert("foobar alert!"))
{ document.getElementById('foobarTune').play();};

to no avail.

I know I can just do:

document.getElementById('foobarTune').play();
alert("foobar alert!")

to get it to run before the alert once.

My main concern is to get it to run during the alert and then have it repeat while the alert box is up. And stop when alert box is dismissed by the user.

I assume you're using html 5 to play the audio. See the loop attribute. Then just call stop() on the line after the alert(). alert() blocks script execution, so it will work.

alert is a blocking call; statement after it won't execute until the alert box has been dismissed.

You would need to start a loop using setTimeout to play your sound before the call to alert . The, once the call to alert completes, you could remove your timer / task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM