我有一个现有函数,可将信息成功加载到div中。看起来像这样。
<div id="load_warnings">
<!-- this is where the warnings will populate from ajax request -->
</div>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load_warnings').load('https://www.gastromend.com/warnings.php').fadeIn("slow");
}, 20000);
</script>
问题:
当此信息加载到div中时,我还想通过播放声音通知来进一步增强此功能。我正在努力了解如何在此设置中使用方法链jquery
.play方法。
我较早尝试过此方法,但没有成功-没有音频播放。
<div id="load_warnings">
<!-- this is where the warnings will populate from ajax request -->
</div>
<script type="text/javascript">
var audioUrl = 'https://wwww.gastromend.com/resources/uploads/condodge_warning.mp3'; // declaration of audio path
var audio = new Audio(audioUrl); // instantiation of audio object
var auto_refresh = setInterval(
function ()
{
$('#load_warnings').load('https://www.gastromend.com/warnings.php').fadeIn("slow").audio.play();
}, 20000);
</script>
答案 0 :(得分:2)
audio
AJAX请求完成后,您需要告诉load()
元素播放。为此,请在play()
的回调参数中调用load()
方法:
var audio = new Audio(audioUrl);
var auto_refresh = setInterval(function() {
var $el = $('#load_warnings').load('https://www.gastromend.com/warnings.php', function() {
audio.play();
$el.fadeIn('slow');
});
}, 20000);