0

My script works if I make changes to the data.txt file right away, but if I let the browser sit for a few min. and I change the content in data.txt file again, it does not work anymore till I refresh the browser.

index.php

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<meta name="robots" content="noindex, nofollow" />
<title></title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
var timestamp = null;
function getMsg() {
$.ajax({
    type: "GET",
    url: "get.php?timestamp=" + timestamp,
    async: true,
    cache: false,
    success: function(data) {
        var json = eval('(' + data + ')');
        if (json['msg'] != "") {
            $(".chat").append(json['msg'], "<br>");
        }
        timestamp = json['timestamp'];
        setTimeout('getMsg()', 1000);
    }
});
}
$(document).ready(function() {
getMsg();
});
</script>
</head>
<body>
<div class="chat"></div>
</body>
</html>

get.php

<?php

$file = "data.txt";
$lastmoded = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$currentmoded = filemtime($file);

while ($currentmoded <= $lastmoded) {
usleep(10000);
clearstatcache();
$currentmoded = filemtime($file);
}

$response = array();
$response['msg'] = file_get_contents($file);
$response['timestamp'] = $currentmoded;
echo json_encode($response);

?>
  • json['msg'] use like json.msg. And add dataType:'json', – Krish R Dec 07 '13 at 06:39
  • Just out of curiosity, what happens if you remove your setTimeout in the success callback and just do `$(document).ready(function() { setInterval(getMsg, 1000); });` – Rob M. Dec 07 '13 at 06:41
  • Check php error log as it may have a 30sec timeout, so if your php script doesn't finish its job in that time then it dies. – bumbu Dec 07 '13 at 06:49
  • Thanks bumbu I believe that worked. – user2094315 Dec 07 '13 at 07:03
  • Btw I've made something similar, maybe this code helps you: https://github.com/panique/php-long-polling – Sliq Dec 26 '13 at 05:17

2 Answers2

0

Try

var timestamp = null;

$(document).ready(function() {
  getMsg();
  function getMsg() {
    $.ajax({
      type: "GET",
      url: "get.php?timestamp=" + timestamp,
      async: true,
      cache: false,
      success: function(data) {
          var json = eval('(' + data + ')');
          if (json['msg'] != "") {
              $(".chat").append(json['msg'], "<br>");
          }
          timestamp = json['timestamp'];
         setTimeout('getMsg()', 1000);
      }
  });
  }
});
0

The server has probably passed an error back, you can restart the function if you get an error

function getMsg() {
$.ajax({
    type: "GET",
    url: "get.php?timestamp=" + timestamp,
    async: true,
    cache: false,
    success: function(data) {
        var json = eval('(' + data + ')');
        if (json['msg'] != "") {
            $(".chat").append(json['msg'], "<br>");
        }
        timestamp = json['timestamp'];
        setTimeout('getMsg()', 1000);
    },
    error: function(){
       setTimeout('getMsg()', 1000);
    }
});
}
Moshe Katz
  • 15,992
  • 7
  • 69
  • 116