在setInterval中包含一些代码只执行一次

时间:2014-01-31 23:55:41

标签: javascript jquery ajax

我在Javascript中进行了setInterval调用,检查是否有新的用户通知。此间隔进行AJAX调用并根据响应更新DOM。此间隔设置为每10秒重复一次。

如果有新通知并且在此间隔内,则需要弹出一个小框。在当前代码中,如果有新通知没有标记为 see ,则此框每10秒显示一次,这非常烦人。

有没有办法让每个通知集只出现一次这个小框?因此,例如有X个新通知,10秒后这个数字没有改变,不显示此框。我该如何实现这一目标?我被困在这里。

这是我的间隔代码的样子:

setInterval(function(){
    $.get(generate_site_url() + 'user.php?action=get_notifications', function(data) {

        response = $.parseJSON(data);

        if ('error' in response)
        {
            return; 
        }

        if (response.notification_array.length == 0)
        {
            return; 
        }

        $('.user-notification').text(response.notification_count);
        $('.no-notes').hide();

        var notificationStr = '';

        for (var key in response.notification_array) 
        {
            var notification = response.notification_array[key];
            var notificationHTML = '<li' + ((notification.notification_seen == false) ? ' style="background: #fffaf1;"' : '') + '>';
            notificationHTML += '<a href="' + notification.notification_target + '" id="nid-' + notification.notification_id + '">';
            notificationHTML += '<span class="glyphicon glyphicon-' + ((notification.notification_type == 'like') ? 'thumbs-up' : (notification.notification_type == 'dislike') ? 'thumbs-down' : (notification.notification_type == 'favorite') ? 'heart' : 'bell') + '"></span>&nbsp;&nbsp;';
            notificationHTML += notification.notification_message;
            notificationHTML += '</a></li>';

            notificationStr += notification.notification_message + '<br />';

            $('.notifications-dropdown').prepend($(notificationHTML));
        }

        display_alert(notificationStr, 'danger', 5000, 'bottom'); // This shows the box
    });
}, 10000);

3 个答案:

答案 0 :(得分:1)

我会在这里扩展我原来的评论答案。

设置可在区间函数外部访问的变量,您可以在其中跟踪新通知的最后计数。下次间隔运行时,比较计数并检查是否有新的计数。

var lastNewMessageCount = 0;
setInterval(function(){

  // ajax stuff
  if( response.notification_array.length > lastNewMessageCount ){
    // show notices

  }
  lastNewMessageCount = response.notification_array.length; 

});

答案 1 :(得分:0)

尝试创建一个全局数组,然后在执行if(response.notification_array.length > nameOfGlobalArray.length)之前添加条件display_alert(),如果条件返回true,则更新nameOfGlobalArray以匹配response.notification_array,如:< / p>

var notificationsArray = [];
setInterval(function(){
    $.get(generate_site_url() + 'user.php?action=get_notifications', function(data) {

        response = $.parseJSON(data);

        if ('error' in response)
        {
            return; 
        }

        if (response.notification_array.length == 0)
        {
            return; 
        }

        $('.user-notification').text(response.notification_count);
        $('.no-notes').hide();

        var notificationStr = '';

        for (var key in response.notification_array) 
        {
            var notification = response.notification_array[key];
            var notificationHTML = '<li' + ((notification.notification_seen == false) ? ' style="background: #fffaf1;"' : '') + '>';
            notificationHTML += '<a href="' + notification.notification_target + '" id="nid-' + notification.notification_id + '">';
            notificationHTML += '<span class="glyphicon glyphicon-' + ((notification.notification_type == 'like') ? 'thumbs-up' : (notification.notification_type == 'dislike') ? 'thumbs-down' : (notification.notification_type == 'favorite') ? 'heart' : 'bell') + '"></span>&nbsp;&nbsp;';
            notificationHTML += notification.notification_message;
            notificationHTML += '</a></li>';

            notificationStr += notification.notification_message + '<br />';

            $('.notifications-dropdown').prepend($(notificationHTML));
        }

        if(response.notification_array.length > notificationsArray.length)
        {
            display_alert(notificationStr, 'danger', 5000, 'bottom');
            notificationsArray = response.notification_array;
        }
    });
}, 10000);

[编辑] BotskoNet 的方法使用较少的数据,显然我的大脑没有打开:P两者都可以工作。

答案 2 :(得分:0)

user.php会在发送后标记通知吗?我会假设没有,但如果是这样,你只需要检查是否有新通知,如果有的话只调用display_alert()

跟踪通知计数或比较字符串是不够的。有很多用例会导致误报。但我发现有一个notification_id字段:

var delivered= [];
setInterval(function(){

  // ajax stuff
  for (var key in response.notification_array){
     var notification = response.notification_array[key];

     // check if the notification has been delivered
     if ($.inArray(notification.notification_id, delivered) === -1){ 
         // notification has not been delivered 
         delivered.push(notification.notification_id);

         // process notification as normal
     }
  }

  // only display the alert if there is something to display...
  if (notificationStr.length > 0) 
      display_alert(...);

}, 10000);