Ajax 请求仪表板页面上的浏览器崩溃

Ajax requests crashing browser on dashboard page

本文关键字:浏览器 崩溃 请求 仪表板 Ajax      更新时间:2023-09-26

我有一个仪表板页面,用于检查更新的 json 响应是否显示在页面上。页面和站点的其余部分与显示它的浏览器托管在同一台计算机上。它旨在保留在该页面上,并连续保留在该页面上。它由两部分组成,一个javascript/jquery提取器,它调用一个PHP页面,如果内容发生了变化,则读取返回"有效负载"值的数据库,如果没有,则只返回检查的最后一个时间戳。它目前可以拉动笔记,因为它们会发生变化,但是它有问题

确定问题:

1:在 chrome 控制台中,每次加载页面时都会收到此消息:

主线程上的同步 XMLHttpRequest 已被弃用,因为它会对最终用户的体验产生不利影响。如需更多帮助,请查看 http://xhr.spec.whatwg.org/。

2:我注意到将其添加到页面后浏览器使用的RAM量大幅增加。大约 4 小时后,浏览器崩溃。

JS代码:

var lastcheck;
var content_main = $('#notes');
$(document).ready(function() {
        $.ajaxSetup({ cache: false }); 
            setInterval(function() {
                updateJson();
            }, 5000);  
});
function updateJson() {
  var request = '/sections/noteboard/notes.php?new=1&timestamp='+ (lastcheck ? lastcheck : 0);
  $.ajax({
    url: request,
    dataType: 'json',
    async: false,
    cache: false,
    success: function(result) {
      if (result.payload) {        // new data
        lastcheck = result.time;   // update stored timestamp
        content_main.html(result.payload); // update html element
      } else {                     // no new data, update only timestamp
        lastcheck = result.time;
      }
    }
  });
}

PHP代码:

$timestamp = 0;
$where = '';
if (isset($_GET['timestamp'])) {
    $timestamp = $_GET['timestamp'];
}
if ($timestamp) {
 $where = ' WHERE timing >= '.$timestamp;
}
$result = $mysqli->query("SELECT * FROM `mirror_notes` ". $where ." ORDER BY `id` DESC LIMIT 0,1");
$row_cnt = $result->num_rows;
$output = array();
$myrow = $result->fetch_array(MYSQLI_ASSOC);
if ($row_cnt=='1' && $myrow['message'] !== '') {   // do we have any script output ?
    $output['payload'] = stripslashes($myrow['message']);  // your current script output would go in this variable
}
$output['time'] = time();      // so we know when did we last check for payload update
$json = json_encode($output, ((int)JSON_NUMERIC_CHECK)); // jsonify the array
echo $json;                    // send it to the client 
$result->close();
$mysqli->close();
exit();

非常感谢任何帮助,因为我是jquery的初学者,并且只是对php稍微好一点。谢谢

我认为

没有人会让浏览器打开这么长时间崩溃。

一个简单的解决方案是使用套接字。使用 php 打开套接字并开始与浏览器通信并不难。你应该研究一下。