2016-10-11 17 views
0

每隔5秒我從數據庫中爲我的通知提取數據。但是,每5秒鐘通知div即將關閉,並且我希望div保持打開狀態。我怎樣才能做到這一點?我如何禁用div來停止關閉

注意 ajax調用,抓取正在工作。我只是想讓我的通知divsetinterval觸發時停止關閉。

這裏的div

enter image description here

home.php

<div id="notificationsss"> 
      </div> 



     $(document).ready(function() 

     { 

      loadnotif(); 
      setInterval(loadnotif, 5000); 

      $("#notificationsss").on("click",$("#notificationLink"), function() { 
      $("#notificationContainer").fadeToggle(300); 
     $("#notification_count").fadeOut("slow"); 

     }); 




     }); 

function loadnotif(){ 

       $.ajax({ 
       url:'getrecords.php', 
       method:'POST', 
       data:{ 
        "loadnotif": 1 
       }, 
       success:function(data){ 
        $('#notificationsss').html(data); 
       } 
       }); 
      } 

getrecords.php

if(isset($_POST['loadnotif'])){ 

        $sql = "SELECT * FROM notification"; 
          $result = mysqli_query($con,$sql); 
          $count = mysqli_num_rows($result); 


      $output = '  <ul id="main-menu" class="nav navbar-nav navbar-right"> 
        <li class="dropdown hidden-xs"> 
        <li id="notification_li"> 
        <a href="#" id="notificationLink"><i class="fa fa-globe"></i>  
         <span class="notification-counter" style="background-color:red;border-radius:3px;padding: 1px 3px;position:relative;top:-9px;right:9px;font: 8px Verdana;;">'.$count.'</span></a> 

        <div id="notificationContainer"> 
        <div id="notificationTitle" style="text-align:center;background-color:#ba4f46;color:#fff;">Notifications</div> 
        <div id="notificationsBody" class="notifications">'; 


         while($row = mysqli_fetch_array($result)){ 

      $output .='  <a href="viewlecture.php?subjdescr='.$row['subj_descr'].'" style="display:block;color:black;margin-top:10px;background-color:#f6e9e8;" id="notifa"> 
          <div> 
          <img src="img/izuku.jpg" style="max-width:50px;max-height:70px;float:left;margin:0px 10px;"> 
          <p style="display:inline;margin-top:20px;"><strong>'.$row['fac_code'] .'</strong> '.$row['notif_description'].'<strong><br> '.ucwords(strtolower($row['subj_descr'])).'</strong></p> 
          <p style="font-size:12px;">'.$row['date'].'</p> 
           <hr> 
          </div> 

         </a>'; 
         } 


     $output .='   </div> 
        <div id="notificationFooter" style="background-color:#ba4f46;"><a href="#" style="color:#fff;">See All</a></div> 
        </div> 
        </li> 
      </li> 


      </ul>'; 

      echo $output; 

    } 
+0

什麼元素是這樣的:$( 「#notificationsss」)?它不在那裏。 –

+0

哦。抱歉。這是一個div,我把通知div。 – wataru

回答

0

我測試&發現ok了。你有沒有把你的ajax叫做div裏面的getrecords.php文件?這個過程是 - 它會調用該文件&從中收集信息&替換您的首選div內的數據。

page1.php中

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<script> 
    $(document).ready(function() 

    { 

     loadnotif(); 
     setInterval(loadnotif, 5000); 

     $("#notificationsss").on("click",$("#notificationLink"), function() { 
     $("#notificationContainer").fadeToggle(300); 
    $("#notification_count").fadeOut("slow"); 

    }); 




    }); 

function loadnotif(){ 

      $.ajax({ 
      url:'getrecords.php', 
      type:'POST', 
      data:{ 
       "loadnotif1": 1 
      }, 
      success:function(data){ 

       $('#notificationsss').html(data); 
      } 
      }); 
     } 

<div id="notificationsss"> 

</div> 

getrecords.php應該是這樣的

if(isset($_POST['loadnotif'])){ 

       $sql = "SELECT * FROM notification"; 
         $result = mysqli_query($con,$sql); 
         $count = mysqli_num_rows($result); 


     $output = '  <ul id="main-menu" class="nav navbar-nav navbar-right"> 
       <li class="dropdown hidden-xs"> 
       <li id="notification_li"> 
       <a href="#" id="notificationLink"><i class="fa fa-globe"></i>  
        <span class="notification-counter" style="background-color:red;border-radius:3px;padding: 1px 3px;position:relative;top:-9px;right:9px;font: 8px Verdana;;">'.$count.'</span></a> 

       <div id="notificationContainer"> 
       <div id="notificationTitle" style="text-align:center;background-color:#ba4f46;color:#fff;">Notifications</div> 
       <div id="notificationsBody" class="notifications">'; 


        while($row = mysqli_fetch_array($result)){ 

     $output .='  <a href="viewlecture.php?subjdescr='.$row['subj_descr'].'" style="display:block;color:black;margin-top:10px;background-color:#f6e9e8;" id="notifa"> 
         <div> 
         <img src="img/izuku.jpg" style="max-width:50px;max-height:70px;float:left;margin:0px 10px;"> 
         <p style="display:inline;margin-top:20px;"><strong>'.$row['fac_code'] .'</strong> '.$row['notif_description'].'<strong><br> '.ucwords(strtolower($row['subj_descr'])).'</strong></p> 
         <p style="font-size:12px;">'.$row['date'].'</p> 
          <hr> 
         </div> 

        </a>'; 
        } 


    $output .='   </div> 
       <div id="notificationFooter" style="background-color:#ba4f46;"><a href="#" style="color:#fff;">See All</a></div> 
       </div> 
       </li> 
     </li> 


     </ul>'; 

     echo $output; 

    } 
+0

抓取工作正常。但我的問題是,每當我在5秒內獲取數據時,div都會自動關閉。 – wataru

+0

你把你的 '

'?把它放在第一頁。我已經使用了兩頁(假設page1.php,我已經把我的腳本'
')和另一個頁面命名爲getrecords.php。 – Jobayer

+0

你不明白我的問題先生。 – wataru