I'm working on a php-ajax chat, and I use two main methods: "update" and "send"
Update function (ajax):
$.ajax({
type: 'POST',
timeout: 9999999999999999,
url: 'process.php',
success: ...
});
Process.php:
function update(){
if(new messages){
echo new messages...
}else{
sleep(5);update(); again...
}
}
Send function (ajax):
$.ajax({
type: "POST",
url: "send.php",
success: function(){
update();}
})
I want to stop running "update" request when sending new message and start it again immediently, so sender will instantly see his message. I have tried ajax.abort() but it can still continue to execute the call on server side.
How can I complete the ajax call and make return success, not aborting it?
In your PHP code you could check for connection aborted http://php.net/manual/en/function.connection-aborted.php
But if you realy want to return "success" I would add something to the session in send.php like
$_SESSION['SENDNOW']=true;
and then in the Process.php I would check for
if(new messages || $_SESSION['SENDNOW']==true){
$_SESSION['SENDNOW']=false;
}
perhaps you should make the timeout 1sec for quicker response times. Or if you do it perfectly you could check quickly (like every 500ms) for $_SESSION['SENDNOW']==true and only every 5seconds for new messages
Please try this:
First document.ready() function declares an Ajax call as variable like below:
var ajaxCall = null;
Put this code in within your AJAX function at top:
//AJAX request aborted
if ( typeof ajaxCall != 'undefined' ) {
return false;
}
For call to AJAX like this:
ajaxCall = $.ajax({
type: 'POST',
timeout: 9999999999999999,
url: 'process.php',
success: ...
});
So every time not call to server.