Jquery “如何获取通知声音”仅在有新数据输入时读取一次
我有一个代码,代码工作,但声音总是在运行/播放,我希望声音只要播放一次,如果有新的数据输入,但不能。我希望我的朋友能帮助我。thnkJquery “如何获取通知声音”仅在有新数据输入时读取一次,jquery,Jquery,我有一个代码,代码工作,但声音总是在运行/播放,我希望声音只要播放一次,如果有新的数据输入,但不能。我希望我的朋友能帮助我。thnk function notification(){ $('<audio id="chatAudio"><source src="../sound/notify.ogg" type="audio/ogg"><source src="../sound/notify.mp3" type=
function notification(){
$('<audio id="chatAudio"><source src="../sound/notify.ogg" type="audio/ogg"><source
src="../sound/notify.mp3" type="audio/mpeg"><source src="../sound/notify.wav"
type="audio/wav"></audio>').appendTo('body');
$.ajax({
url:"check.php",
chace: false,
success: function(data){
if(data == 0){
$(".place").hide();
}else{
$(".place").show("fast");
$(".place").html(data);
$('#chatAudio')[0].play();
}
}
}).always(function(){
setTimeout(function(){notification()}, 3000);
});
}
$(document).ready(function(){
notification();
});
如果将音频元素的添加移到通知函数的外部,使其只添加一次,那么代码看起来会起作用。我已经对你的代码进行了一些重构,它应该可以正常工作。只要确保PHP在没有新通知时返回0。我也不确定chace在你的JS中是什么意思
function notification(){
$.ajax({
url:"check.php",
chace: false, //<-- Not sure what this is meant to be?!
success: function(data){
if(data == 0){
$(".place").hide();
}else{
$(".place").show("fast");
$(".place").html(data);
$('#chatAudio')[0].play();
}
}
});
}
$(document).ready(function(){
$('<audio id="chatAudio"><source src="../sound/notify.ogg" type="audio/ogg"><source
src="../sound/notify.mp3" type="audio/mpeg"><source src="../sound/notify.wav"
type="audio/wav"></audio>').appendTo('body');
var notificationInterval = setInterval(function(){notification();}, 3000);
});
您可以在声音对象(即chatAudio)上使用javascript中的.play、.pause方法。有关更多信息,请参阅:@Vedant如何定义将音频元素的添加移出通知功能,因为您当前会多次添加音频元素。嗯,这很好,谢谢您的解决方案。。顺便说一句,在JS的制造过程中,如果有任何会导致内存占用继续?如果我理解正确,你想知道这是否会持续增加内存使用?我不这么认为。如果你担心的话,看看谷歌Chromes开发者工具的Profile标签。