Suppose I make an AJAX HTTP Request from jQuery to a back-end PHP script that does some querying on MySQL server. While the request is made, I want to regenerate a new request and terminate the current one on the server end.
Script front-end.php contains this:
function process(json) {
// iterate and display result
}
google.maps.event.addListener(map, 'idle', function () {
....
$.getJSON('back-end.php?id=$id&bounds=$bounds', process);
});
Script back-end.php contains this:
$id = // get current MySQL process id
// check and send request to terminate previous process if id is set
// send request to get data based on bounds criteria
echo json_encode(data);
How can I get $id onto script front-end.php so that I can terminate the previous process on the server as soon as I issue the next one?
You need to save this process id somewhere common to the two different requests. Something like in a text file, or db table, or memcache on the backend. So for example the first php request starts your process then writes the process id to a file such as current_id.txt. Then the second request checks this file for an id, then kills the process then blanks out current_id.txt after it is killed.
If you need this to be replicable across many users, you have to have a way to identify it is the same person with a session variable or the session id itself, and create files like
<session_id>_process_id.txtso each unique user can do this independent of each other.