Answer the question
In order to leave comments, you need to log in
What is the error in implementing the Comet (Long - Polling) method?
The problem is that the function fires every 0.1 second or so, and doesn't react at all to changing the timeout value. Please suggest what is wrong.
A function is created on the client side:
function getmess(){
$.ajax({
url:"get_mess.php",
data:{"id":id},
type:"POST",
timeout: 100000,
complete:getmess,
success: function(result){
$("#response").html(result);
}
});
}
Loop on server:
while (true) {
$res = mysql_query('SELECT * FROM saymon WHERE id>$_POST['id']');
if (mysql_num_rows($res)) {
while ($item=mysql_fetch_array($res)) {
echo $item['mess']."";
}
flush();
exit;
}
sleep(5);
}
Answer the question
In order to leave comments, you need to log in
It's not clear what you need.
1. Wait 100000ms for the server's response and if it doesn't respond, then resend the request,
or
2. Send the request with a delay of 100000ms after receiving the previous response?
In the first case, js works fine - checked.
Perhaps php does not work correctly and gives you an answer within 0.1s. Output logging, output echo with different conditions.
In the second, you need to rewrite the code a bit:
function getmess(){
$.ajax({
url:"get_mess.php",
data:{"id":id},
type:"GET",
success:function(result){
$("#response").html(result);
setTimeout('getmess',100000);
}
});
}
Regarding the mechanics on the client, Serkaz wrote everything correctly.
If we omit the time for requests, then through setInterval () you can evenly call this function.
And the problem was in
complete:getmess
On event of comolete of request you next called this function again. Hence the interval of 0.1 sec was
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question