![](/img/trans.png)
[英]How can I repeatedly play a sound while a javascript alert message is up and stop it when the alert message is dismissed?
[英]Play audio when javascript alert pops up and stop playing when the alert closes
我正在尝试在弹出 javascript 警报时播放音频,并在用户关闭警报时停止播放音频。 但是当用户关闭警报时,音频并没有停止。即使在我关闭它之后,警报也会一次又一次地出现。
const audio = new Audio("sounds/Sparkle - Your Name.mp3");
audio.play();
audio.loop = false;
if(!alert("Time for some anime")){
audio.pause();
}
你可以使用confirm()
而不是alert()
const audio = new Audio("sounds/Sparkle - Your Name.mp3");
audio.play();
audio.loop = false;
function after() {
audio.pause();
}
if(confirm("Time for some anime"))
after();
else
after();
或更短的confirm("continue to pause")? after(): after();
confirm("continue to pause")? after(): after();
你可以在这里测试它:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.