【发布时间】:2017-02-15 11:25:56
【问题描述】:
我有以下代码:
var player = document.createElement('audio');
player.src = 'https://dl.dropbox.com/u/7079101/coin.mp3';
player.preload = 'auto';
var url= 'http://www.businessdictionary.com/definition/update.html';
window.setInterval(function(){
if($("*:contains(Update')").length > 0){
console.log('New Update, playing sound...');
player.play();
}
}, 5000);
致谢:劳伦斯·斯沃特
代码运行良好,但音频会一直循环播放。有没有办法让音频只播放一次?
【问题讨论】:
-
使用
setTimeout()而不是setInterval -
通过帮助您,我可能对我所在国家/地区的当局有一些问题,所以这将作为评论保留,但如果目标是检查一个包含
Update的元素何时添加到文档中,并在此时播放声音,那么您就知道setInterval确实返回一个id,当您的检查通过时,它与clearInterval方法一起使用。如果您正在等待多个此类事件,请将> 0更改为> current并在每个新的积极事件时增加current。 -
@Kim Jong-un 一旦你确定了这个......如果你这样做了......发布一个答案来帮助下一个人......欢迎来到 SO
标签: javascript jquery loops audio