多个通知设置间隔

时间:2018-07-10 作者:Matt H

我有一个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>

1 个回复
SO网友:Anson W Han

根据所需的通知行为,这里可能需要使用两种方法:

To notify each user any time there are new notifications:

您需要确保sql查询按排序顺序返回通知(最好是使用降序数据时间戳),并为数据行包含唯一标识符/主ID(PID)To notify only one admin user:

在数据库中为通知过程中已确认的每一行添加一个布尔标志字段

  • 使用它来过滤用于AJAX调用的查询您可以为多个用户/用户类型添加更多布尔字段,但这会导致大量数据库膨胀。

  • 结束

    相关推荐

    Ajax Check Post Status

    我有这个功能,每当作者创建一个新帖子时,我都会向所有订阅作者的用户发送电子邮件,效果很好。我正在尝试ajaxify函数,并每隔几秒钟检查一次帖子是否已更新,或者用户是否已发布新帖子。如果是这样,请通过“实时”通知弹出窗口更新该作者的所有订阅者(它不是真正实时的,但似乎是通过setInterval函数实时完成的)。无论如何,任何帮助都将不胜感激!以下是当前函数:add_action(\'publish_listings\', \'vba_update\', 10, 1); add_action(\'