weixin_33743661 2016-01-17 07:10 采纳率: 0%
浏览 36

AJAX setInterval错误

kindly check sample here in this link: My test code

This will be used on a div on my actual site and had same problems. As you can see each 2 seconds the div blinks or in my test code site the whole page then when you scroll down it will scroll up automatically with the same time. Obviously it was caused by the setInterval.

Also I'm getting console error:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.

Is there any way to improve this?

Also, are there other ways to present updated list from database with ui in every second?

Here are my codes:

<div id="container"></div>
<script>
    function refreshNews()
    {   $.get('../php/ajaxreportall.php').success(function(data){
             $("#container").html(data);
        })
    }
    refreshNews();
    setInterval(refreshNews, 2000);
</script>

the script file

<?php 
    require("database.php");

    $sql = "SELECT * from Report r inner join reddb.User_Info u 
            on u.user_email = r.user_email ";

    $res = odbc_exec($conn,$sql);

    echo "
            <link rel='stylesheet' type='text/css' href='../css/homelayout.css'>
            <script src='../js/home.js'></script>
        ";

    while($feedItem = odbc_fetch_array($res))
    {
        echo "
            <div class='feedItem'>
                <div>Report No. <label class='feedItemRepID'>" . $feedItem['report_id'] . "</label></div>
                <div>Name:  <label>" . $feedItem['firstname'] . "</label></div>
                <div>Contact No: <label>" . $feedItem['contact'] . "</label></div>
                <div>Details:  <label>" . $feedItem['report_detail'] . "</label></div>
                <div>Date: <label>" . $feedItem['date_report'] . "</label></div>
                <div class='buttons'>
                    <button class='btn btn-success btnRespond' data-toggle='modal' data-target='#myModal'>Respond</button>
                </div>
            </div>
            ";

    }

?>
  • 写回答

2条回答 默认 最新

  • larry*wei 2016-01-17 07:20
    关注

    It is not a good idea to use interval to Ajax. Instead change to this:

    function refreshNews() {   
      $.get('../php/ajaxreportall.php').success(function(data){
        $("#container").html(data);
        setTimeout(refreshNews, 2000);
      });
    }
    $(function() {
      refreshNews();
    });  
    

    or test if the data changed:

    var rfn="";
    function refreshNews() {   
      $.get('../php/ajaxreportall.php').success(function(data){
        if (rfn!=data) {
          $("#container").fadeOut(function() {
            $("#container").html(data);
            $("#container").fadeIn();
          });
          rfn=data;
        }
        setTimeout(refreshNews, 2000);
      });
    }
    $(function() {
      refreshNews();
    });  
    

    If you want the above to never stop, even when failing, use .always

    function refreshNews() { 
      var jqxhr = $.ajax('../php/ajaxreportall.php')
      .done(function(data){
        if (rfn!=data) {
          $("#container").fadeOut(function() {
            $("#container").html(data);
            $("#container").fadeIn();
          });
          rfn=data;
        }
      })
      .fail(function() {
        console.log( "error" );
      })
      .always(function() {
        setTimeout(refreshNews, 2000);
      });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考