I have a long running query (think Comet/Remoting) and I want to check if that connection has gone down.
Sometimes there is a server problem and I want the user to stop using the GUI and refresh it.
I pass a function into $.ajax to be called when the query completes.
I then check the status and if the call is not complete then I display a message. The code is shown below:
- completeFun = function(data, status) {
- var Fun;
- if (status !== "success" && status !== "abort") {
- Fun = function () {
- HN.Util.showDeadBox("Connection with the server " +
- "has been lost. Please refresh " +
- "the page in your browser");
- };
- setTimeout(Fun, 300);
- }
- };
-
- if (updateRequest === null) {
- f = function (path) {
- return path !== rootPath;
- };
- paths = $.grep(api.getLoadedUrls(), f).join(",");
- args = {"updates": time, "paths": paths, "view": tpl};
- setTimeout(function () {
- updateRequest = $.ajax({"data" : args,
- "url" : rootPath,
- "dataType" : "json",
- "success" : dataSuccessFun,
- "complete" : completeFun});
- }, 0);
The problem is that the complete function is also trigged when someone navigates away from the page.
In the older versions of JQuery the status was different in the two cases where the ajax failed as a result of a server failure and a navigate away. Now they both return the same status.
I have tried to set a global flag to determine if the page is unloading by binding functions on window.unload, window.abort, document.unload and document.abort but none of these fire before the ajax query is aborted.
So my question is where and how in the event cycle does JQuery abort long running ajax queries and how can I get into that part of the event cycle to:
* either ensure that I get a different status on my ajax abort
* or set a global 'hey I am unloading" flag that I can test before displaying the box
I have experimented with increasing the timeout on the box display because (sometimes) the whole page has gone away before the box tries to display and it don't, but that is a bit shonky (and variable).