0

i use the following script to start a long poll with the php file.. it checks if any results are updated and sends a response ..

For some reason when this javascript is inserted all other scripts hangs on a long poll on fire bug

function waitForMsg(){
$.ajax({
    type: "GET",
    url: "auth/classes/getdata.php",
    async: true,
    cache: false,

    success: function(data){

    console.log(data)

    setTimeout("waitForMsg()",1000);
    },
    error: function(XMLHttpRequest,textStatus,errorThrown) {
//   alert("error: "+textStatus + "  "+ errorThrown  );
    setTimeout("waitForMsg()",15000);
    }
  });
}


$(document).ready(
 function() 
 {  

  waitForMsg();
 });

This is the php file getdata.php

require_once($_SERVER['DOCUMENT_ROOT'].'/auth/config/db.php');
 $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

$user_id = $_SESSION['user_id']; 
$lastmodif = time();
$update = 1;
while ($update <= $lastmodif) {
usleep(10000);
clearstatcache();
 $sql = "select ua.user_id as member,ua.post_id,pa.user_id,pa.type,pa.time,CONCAT(u.first_name,' ',u.last_name) as
 name,u.thumbnail from user_activity ua right join post_activity pa on 
 ua.post_id=pa.post_id right join users u on pa.user_id=u.user_id where
  ua.user_id=".$user_id." and pa.time > FROM_UNIXTIME('".$lastmodif."')";
  $result = $conn->query($sql) or die(mysqli_error());
 if ($conn->affected_rows > 0) {
$update=$lastmodif;
$response = array();
$response['msg'] ='update';
echo json_encode($response);

 }
}

2 Answers2

1

Pretty sure your problem is

usleep(10000);

this will effectively stop execution and the ajax-cycle you try to initiate with setTimeout("waitForMsg()",1000); - usleep blocks for the execution logic.

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • removed usleep.. but still doesnt work.. i inserted the script without executing it in html.. everything loads fine.. the moment i run waitForMsg(); on fire bug.. all other ajaxes stops working and hangs – user3038421 Dec 10 '13 at 12:42
-1
<?

 if(!define("_IS_GET"))
exit("Use another form to get get awww");
else
{
ignore_user_abort(TRUE);
set_time_limit(0); // adjust this to trap long time load
//...your lame process here

//sample you wanna dogetdata.php

$prId = shell_exec("nohup -f php '/path/to/your/getdata.php' /dev/null 2&<1 & $!",$display);

while(exec("$prId -s"))
echo $display."\n<BR>";

// note killing your jobs can be done using prId so its your initiative to store it and keep it ?
}
?>
cHao
  • 84,970
  • 20
  • 145
  • 172
i am ArbZ
  • 69
  • 3