我有一个ajax函数,它每30秒检查一次数据库中的新通知,并在发现通知时播放通知声音,这很好,但问题是它每次都会收到通知并播放声音,而不是仅在第一次找到通知时,我不确定如何使用setTimeout函数来实现这一点。我尝试了localstorage获取和设置项的方法,但没有成功。。。任何帮助都将不胜感激!
以下是ajax函数:
<script>
function show_notifications(){
var command = "FETCH_NOTIFICATIONS";
var user_id = "<?php echo $my_user_id; ?>";
var alerted = localStorage.getItem(\'alerted\') || \'\';
$( document ).ready(function() {
$.ajax({
url: ajaxurl,
cache: false,
dataType: \'JSON\',
data: {action: \'update_db_ajax\', command: command, user_id: user_id},
success: function(result) {
if(result.sql > 0) {
$(".latest-notifications").remove();
$("#notification_count").text(result.sql);
$("#notification_count").removeClass(\'no-new-notifications\');
for(var i in result) {
if(result[i] > result[4]) {
console.log(result[i].id);
if (alerted != result[i].id) {
$(\'#chatAudio\')[0].play();
iziToast.show({
message: result[i].notification_text,
messageColor: \'#424242\',
backgroundColor: \'#fff\',
theme: \'light\',
animateInside: true,
layout: 1,
close: false,
position: \'bottomLeft\',
timeout: 5000,
progressBar: false
});
localStorage.setItem(\'alerted\', result[i].id);
}
}
}
if(typeof result[4] == "undefined") {
} else {
if(result[4].is_viewed === \'NO\') {
$(".latest-notifications-holder").append(\'<div class="latest-notifications">\' + result[4].notification_text + \' <div class="notification-time"><abbr class="timeago" title="\' + result[4].creation_date_time +\'"></abbr></div> </div>\');
} else if(result[4].is_viewed == \'YES\') {
$(".latest-notifications-holder").text(\'No new notifications found.\');
}
}
if(typeof result[3] == "undefined") {
} else {
if(result[3].is_viewed === \'NO\') {
$(".latest-notifications-holder").append(\'<div class="latest-notifications">\' + result[3].notification_text + \' <div class="notification-time"><abbr class="timeago" title="\' + result[3].creation_date_time +\'"></abbr></div> </div>\');
} else if(result[3].is_viewed == \'YES\') {
$(".latest-notifications-holder").text(\'No new notifications found.\');
}
}
if(typeof result[2] == "undefined") {
} else {
if(result[2].is_viewed === \'NO\') {
$(".latest-notifications-holder").append(\'<div class="latest-notifications">\' + result[2].notification_text + \' <div class="notification-time"><abbr class="timeago" title="\' + result[2].creation_date_time +\'"></abbr></div> </div>\');
} else if(result[2].is_viewed == \'YES\') {
$(".latest-notifications-holder").text(\'No new notifications found.\');
}
}
if(typeof result[1] == "undefined") {
} else {
if(result[1].is_viewed === \'NO\') {
$(".latest-notifications-holder").append(\'<div class="latest-notifications">\' + result[1].notification_text + \' <div class="notification-time"><abbr class="timeago" title="\' + result[1].creation_date_time +\'"></abbr></div> </div>\');
} else if(result[1].is_viewed == \'YES\') {
$(".latest-notifications-holder").text(\'No new notifications found.\');
}
}
if(typeof result[0] == "undefined") {
} else {
if(result[0].is_viewed === \'NO\') {
$(".latest-notifications-holder").append(\'<div class="latest-notifications"><a href="<?php echo $post_url; ?>">\' + result[0].notification_text + \'</a> <div class="notification-time"><abbr class="timeago" title="\' + result[0].creation_date_time +\'"></abbr></div> </div>\');
} else if(result[0].is_viewed == \'YES\') {
$(".latest-notifications-holder").text(\'No new notifications found.\');
}
}
} else {
$("#notification_count").addClass(\'no-new-notifications\');
}
},
complete: function() {
$(\'.timeago\').timeago();
}
});
});
} //function show_notifications
function startTimer(){
//call show_notifications
show_notifications();
//then start interval
setInterval(show_notifications, 30000);
} //function startTimer()
</script>