0

Im using a similar script as described here: long-polling info from mysql not working on my website, its work but, when i either refresh the page or click a different link i get an error alert+the website starts lagging seriously, can some one tell me what is causing this?

my code:

$oldIDq = mysql_query("SELECT * FROM messages ORDER BY id DESC LIMIT 1");
while($oldrow = mysql_fetch_array($oldIDq)){
$oldID = $oldrow['id'];    
}

$func = '
var oldID = '.$oldID.';

function wait() {
$.ajax({
    type: "GET",
    url: "../scripts/msg_scripts/msg.php?oldid=" + oldID,
    async: true,
    cache: false,

    success: function (data){
     var json = eval(\'(\' + data + \')\');  

     if (json[\'msg_content\'] != "") {
      alert("new meassage added");   
     } 

     oldID = json[\'oldID\'];
     setTimeout(\'wait()\',1000);
    },

    error: function(XMLHttpRequest, textStatus, errorThrown){
      alert("error: " + textStatus + "(" + errorThrown + ")");  
      setTimeout(\'wait()\',15000);
    }

});
}

$(document).ready(function(){

    wait();
});
';

server:

<?php 
    session_start();
    $connect = mysql_connect ("localhost", "root", "")

or die ("couldnt connect");
mysql_select_db ("***") or die ("not found"); //if db was   not found die
mysql_query("SET NAMES 'utf8'");

$oldID = $_GET['oldid']; 
$result = mysql_query("SELECT id FROM messages ORDER BY id DESC LIMIT 1");
while($row = mysql_fetch_array($result))
{
    $last_msg_id = $row['id']; 
}
while($last_msg_id <= $oldID)
{
    usleep(1000);
    clearstatcache();
    $result = mysql_query("SELECT id FROM messages ORDER BY id DESC LIMIT 1");
    while($row = mysql_fetch_array($result))
    {
        $last_msg_id = $row['id'];
    }
}
$response = array();
$response['msg'] = 'new';
$response['oldID'] = $last_msg_id;
echo json_encode($response);
?>
Community
  • 1
  • 1
durian
  • 510
  • 1
  • 7
  • 24
  • aaaargh! Help! eval === evil use JSON.parse(jsonString) instaid! And you don't even need the parsing! jquery can do it for you if you add the `dataType: 'json'` parameter! – VDP Sep 11 '12 at 12:25

1 Answers1

0

proplem in here in $_GET['oldid'] when you click on another link or refresh the page without it the variable $oldID will be empty and the sql will fail. and the js function keep calling itself with nothing changed every time an error accured and the ajax will keep calling keep failing wich will result to an infinite loop that will hang your site and i guess if you check it on firebug you will see that happening.

dont now if that what you mean hope it help

AboQutiesh
  • 1,696
  • 2
  • 9
  • 14
  • 1
    you can check if oldid is null if null return -1 and in the js check if the responce = -1 cancel the wait() call – AboQutiesh Sep 11 '12 at 12:37
  • so, on the server side: $oldID = $_GET['oldid']; if($oldID == ""){$oldID == -1}? and on the js? – durian Sep 11 '12 at 12:49
  • @durian No if($oldID == ""){$response=-1} and in the js if(data != '-1'){var json = eval(\'(\' + data + \')\'); if (json[\'msg_content\'] != "") { alert("new meassage added"); } oldID = json[\'oldID\']; setTimeout(\'wait()\',1000); } try this. – AboQutiesh Sep 11 '12 at 12:56
  • @durian ok open your firebug and check how many ajax requests the browser made and their status – AboQutiesh Sep 11 '12 at 13:16
  • i dont use firebug, but i downloaded it now, can you please tell me where should i look for the requests? – durian Sep 11 '12 at 13:41
  • check the console you will find the ajax requests thier in a gray color – AboQutiesh Sep 11 '12 at 13:43
  • what i see is that the get is loading as i enter the page if i refresh or click on a different link it aborts and starts over again – durian Sep 11 '12 at 14:04
  • Thanks a lot u were right, it was doing an endless loop and i finally maneged to solve it with a little more help right here: http://stackoverflow.com/questions/12391813/ajax-long-polling-crashes-at-page-refresh-links-to-another-page – durian Sep 12 '12 at 18:38