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

时间:2016-03-20 22:19:58

标签: javascript php jquery ajax

我有一个仪表板页面,用于检查要在页面上显示的更新的json响应。页面和网站的其余部分与显示它的浏览器托管在同一台计算机上。它旨在不断地保持在该页面上。它由两部分组成,一个javascript / jquery fetcher调用一个PHP页面,该页面读取数据库,如果内容已更改则返回“payload”值,如果不是,则返回检查的最后一个时间戳。当它们改变时,它现在正在拉动音符,但它有问题

好的问题:

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。 感谢

1 个答案:

答案 0 :(得分:0)

我认为没有人会让浏览器长时间处于打开状态。

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