触发警报时播放声音 [英] Have sound play when alert is triggered

查看:123
本文介绍了触发警报时播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在我的javascript中有一个if语句。当它返回true时,它会打开一个警告框并播放警报声。问题是在我按下确定按钮之前声音才会播放。

So I have in my javascript an if statement. When it returns true it opens an alert box and plays an alarm sound. The problem is that the sound doesn't play until I hit the ok button.

以下是相关信息:

  if (x > 10) {
        var snd = new Audio('/alarm.mp3');
        snd.play();
        alert("Thank you!");
    }

理想情况下,我希望播放大约6秒钟的声音,直到它为止最后或直到用户点击关闭对话框。但是在关闭警报框之前确实发出警报就足够了。

Ideally I want the sound which is around 6 seconds long to play until it is at the end or until the user hits closes out of the dialog. But really getting the alarm to sound before closing the alert box would be good enough.

推荐答案

预先在html中预加载音频文件喜欢:

Preload your audio file in the html beforehand like :

<audio id="xyz" src="whatever_you_want.mp3" preload="auto"></audio>

if(x > 10)
{
    document.getElementById('xyz').play();
    alert("Thank you!");
}

这肯定有效。

这篇关于触发警报时播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆