A
A
arcanrun2013-11-21 16:25:16
PHP
arcanrun, 2013-11-21 16:25:16

Why doesn't data arrive instantly in Comet (Long-Polling)?

I can't immediately receive new data - they come only after a new execution of the getmess function, which is executed when the timer expires, what could be the error?

function getmess(){
$.ajax({
url:"get_mess.php",
type:"POST",
data:{"id":id},
cahce:false,
timeout:30000,
async:true,
success:function( result){
$("#response").html(result);
setTimeout('getmess()',10000);
}
});
}

on the server:

while(true){
$a=$_POST["id"];
$find_mess=(mysql_query("SELECT * FROM saymon WHERE id> '$a' "));
if (mysql_num_rows($find_mess)) {
while($row=mysql_fetch_array($find_mess)){
echo $row['mess']."";
}

flush();
exit;
}

sleep(5);
}
mysql_close();

Answer the question

In order to leave comments, you need to log in

4 answer(s)
C
codercat, 2013-11-21
@codercat

Well, you have sleep(5) the same, how do they come instantly? :\

H
himik, 2013-11-21
@himik

where is getmess called the first time?

O
olegmar, 2013-11-21
@olegmar

Well, you have setTimeout with an interval of 10 seconds + another sleep 5 seconds, of course, the data will not come instantly.

M
mayorovp, 2013-11-22
@mayorovp

In vain you use setTimeout to re-request - better use $.ajax({...}).always(getmess)
In this case, re-requests will occur when the old one succeeds - or when it timeouts, and you don't need to follow the magic constants .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question