Question

Not asking how to abort AJAX.

In this case, does anyone know how to prevent the server from transmitting?

For example, in dynamic pagination, a user may scroll 1 line at a time rapidly, causing many requests.

This can easily be cancelled on client-side jQuery with abort, but for those of us pinching pennies on bandwidth allocation, how can we keep the previous requests from transmitting when a new one has been received?

Many thanks in advance!

Specifics

Actually, it's because I'm using the jQuery UI Slider, and I've set the slide event to hit the server.

As you might expect, if you slide that sucker 10% across, the server's just been hit like crazy and is still transmitting all that data even though I've aborted.

That's the true source of the problem since just hitting the up or down button fast probably (in my mind) can't be stopped.

Was it helpful?

Solution

I think I'm overthinking it.

In the dynamic pagination case, I think I can set a $_SESSION equal to microtime(true) at the very beginning of the php and also hold it in a variable.

Right before transmission (or any other resource intensive step) I can check to see if it's still equal. If not, exit.

However, if anyone else can come up with the general purpose solution where you can allow multiple parallel requests, they get the check and +1 from me.

OTHER TIPS

In this case you can consider using Server-sent events. This opens a persistent HTTP connection for transmitting data from server to client. This prevents the client to request data rapidly and waste bandwidth. An example code:

Server-side: (PHP)

<?php
@set_time_limit(0);
header('Content-type: text/event-stream');
header('Transfer-encoding: chunked');
while(true){
    echo 'data:'.$data."/n/n";//todo: send data here
    ob_flush();
    flush();
}
?>

Client-side: (javascript)

var ev = new EventSource('path/to/script');
ev.onmessage = function(e){
    var data = e.data;
    //todo: handle data here
};

Hope this can help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top