duanchi0897 2014-04-10 08:34
浏览 23
已采纳

关于背景PHP的通知

I have a web application that use notification to inform user about anything new (just like Facebook).

My solution is that I send a request every three seconds to check the database if there is anything new to display (jQuery and AJAX). However, this makes the application slow, since a request is sent to check tables every three seconds.

I want to know how to make these notifications work without interrupting the application.
So this is my JS code:

jQuery(document).ready(function(){    

    LoopNotificationCRM();

  });
    function LoopNotificationCRM(){
  setTimeout('LoopNotificationCRM();',3000);
  $.ajax({
            async: false,
            type: "POST",
            url: "controllers/c_ajax_notification.php",
            data: "ordre=check_new_notification",
            success: function(msg){
              if(msg != 'NAN'){
               var t = msg.split('***');
               $('.sNotification').html(t[0]);
               $('.ul-notification').html(t[1]);
               $('.alert-notification').css('display','block');
              }else{
               $('.sNotification').html(0);
               $('.alert-notification').css('display','none');
               $('.ul-notification').html('');
              }
            }, 
          error: function (xhr, status) {  
            alert('Erreur: ' + status); 
            } 
         });
}

And this is my PHP Code:

$notification->getNewNotification("*");
         if($notification->db_num_row != 0){
                      $listNoti = '';
                      while($resN = $notification->fetch_array()){
                       $today = new DateTime(date('Y-m-d H:i:s'));
                       $datNoti = new DateTime($resN['date_not_crm']);

                       $diff = $datNoti->diff($today);
                       if($diff->d == 0){
                         if($diff->h == 0){
                           if($diff->i == 0){
                              $intervale = 'il y a '.$diff->s.' sec';
                           }else{
                              $intervale = 'il y a '.$diff->i.' min';
                           }                           
                         }else{
                           $intervale = 'il y a '.$diff->h.' heure(s)';
                         }
                       }else{
                           $intervale = 'il y a '.$diff->d.' jour(s)';
                       }

       $listNoti .= '<li>  
                                 <a onclick="link(event,\''.$resN['url_not_crm'].'\');updateEtatNoti(this,'.$resN['id_not_crm'].');" style="cursor:pointer;">
                                 <span class="label label-icon label-success"><i class="'.$resN['icon_not_crm'].'"></i></span>
                                 '.$notification->csNotification($resN['description_not_crm']).' 
                                 <span class="time">'.$intervale.'</span>
                                 </a>
                              </li>';
                     }

            echo $notification->getCountNewNotification().'***'.$listNoti;
        }else{
         echo 'NAN';
        }

When I remove the notification code my application become more fast !

  • 写回答

1条回答 默认 最新

  • dpq39825 2014-04-10 08:38
    关注

    If your application is slowing down when you run the ajax every 3 seconds, try simplifying/shrinking the amount of work the page loaded by the ajax needs to do. Rather than reading an entire table, for example, try selecting only notifications with a "read" status of 0. That way, the .php file is faster to execute, meaning the ajax will not slow down the page as much.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序