![](/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.