1

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136

2 Answers2

1

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.

  • Have you already looked into http://php.net/manual/en/features.connection-handling.php? – Rob W Dec 30 '12 at 09:36
  • @RobW Ah, thank-you for the added knowledge! Does jQuery's `ajax` send the required abort code? Is the default PHP setting to `exit` (or equivalent) upon abort? Thanks again! –  Dec 30 '12 at 09:43
  • You have to abort the request itself. To achieve that, you need to store the result of a `$.ajax()` call somewhere, and call `.abort()` on it whenever you create a new request (provided that the variable exists). Whenever the request succeeds or fails, void the variable to free up resources. – Rob W Dec 30 '12 at 09:45
-1

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.

Licson
  • 2,231
  • 18
  • 26
  • SSE are not supported in IE10, by the way. Besides, SSE does not solve the core problem, which is to detect when a request is aborted. http://caniuse.com/#feat=eventsource – Rob W Dec 30 '12 at 09:31
  • If you want the connection to be bi-directional, then you can use websocket but it's hard to set up in php. – Licson Dec 30 '12 at 09:36
  • But SSE can send data when necessary and it only uses a single connection. – Licson Dec 30 '12 at 09:42