My site spans ajax requests that take a while to execute on the server. However, this request becomes invalid if the user makes another request before the first one is completed. In order to save server resources, I'd like to be able to kill the 1st server request when the second one is sent.
I was thinking about polling the session to see if a certain criteria is met. If it is, I'd just abort the current call.
How would I do that? I guess I'd have to launch a second thread when the ajax request starts (on the server side) and that thread would poll the session every 100ms or so. If the criteria is met, it would kill the request.
I'd like to see a code example of this.
People succeed in answering Carl Mercier's questions 23% of the time (5 successes in 22 attempts).
Answers by: Don Miguel de los Platanos | Rich Collins
I am not sure if I am following. Is the issue you being able to determine if there are current ajax request going? If so , you can add a handler to follow these request and kill them if you wish. Here is an example of a handler I use to do something similar.
var myGlobalHandlers = {
onComplete: function() {
if(Ajax.activeRequestCount == sum_num){
// Do Something
}
}
};
Doest that tell the server that the request is no longer valid and that it can terminate?
Anytime you initiate an ajax request using the prototype library, you have various statuses you can poll. In your instance it would be :loading. You can create an event handler to check the number of ajax request. You can elect to shut them down and reiniate a new request in order to manage the request going to the server.
Ok, but if I shut them down, will the server be notified and stop working, or it will just keep going and not return anything to the client at the end because the connection is lost?
No, the server will not know the process is abandonned until it tries to write to the socket. Perhaps you can add inside the handler to execute an action that stops the process you wish.
Yeah, that's what I want to do, but just don't know how.
Maybe should should not allow another request to the server if one is already committed? This way you don't need to worry about trying to kill the process on the server. You can even use the ajax handler above to notify the user that their request is still being processed and to wait. The problem with trying to kill the processs at the server level is trying to determine which thread to kill.
var myGlobalHandlers = {
onLoad: function() {
if(Ajax.activeRequestCount > 1){
alert("Please wait, still process...");
// Stop Ajax Request
stop()
}
}
};
When an ajax call is generated , it will run this function and check to see if there is more than one request. If so it will prompt the user and stop the process.
var myGlobalHandlers = {
onLoad: function() {
if(Ajax.activeRequestCount > 1){
alert("Please wait, still processing...");
// Stop Ajax Request
stop();
}
}
};
fixed.
should be:
onLoaded: function() {
I should use preview more :P
That would just defeat the whole purpose of my site.
Can you explain your site and I might have a better idea of what you are trying to acomplish.