Ignore:
Timestamp:
2023-11-23T20:19:49+13:00 (7 months ago)
Author:
anupama
Message:

Maintaining a list of running AJAX calls, which get aborted if the user navigates away from the page before they've finished. When any call terminates (either sucessfully, with error or on abort) it gets removed from the list of *running* Ajax calls, so only running ones can be aborted.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/web/interfaces/default/js/javascript-global-functions.js

    r38257 r38427  
    55var COMPLETED = 11;
    66var HALTED = 12;
     7
     8var runningAjaxCalls = [];
    79
    810gs.functions = {};
     
    548550    // ajax calls default to using method GET, we want to do POST operations for get-meta and set-meta requests
    549551    // since get-meta-array and especially set-meta-array can be large, e.g. for user comments.
    550     $.ajax({url: url, async: async_setting, type: method, data: data})
     552    var ajaxCall = $.ajax({url: url, async: async_setting, type: method, data: data})
    551553        .done(function(response) {
    552554        ajaxResponse = response;
     
    561563        if(response.indexOf("ERROR: ") !== -1 || response.indexOf("<Error>") !== -1) {
    562564            if(errorResponseFunction != null) {         
    563             console.log("(" + callingFunction + ") Failed with gsdlcgi error:\n" + response);
     565            console.log("!!!! (" + callingFunction + ") Failed with gsdlcgi error:\n" + response);
    564566            errorResponseFunction(response);
    565567            } else {
    566             alert("(" + callingFunction + ") Failed\n" + response);
    567             console.log("(" + callingFunction + ") Failed\n" + response);
    568             }
    569            
     568            alert("@@@ (" + callingFunction + ") Failed\n" + response);
     569            console.log("@@@ (" + callingFunction + ") Failed\n" + response);
     570            }           
    570571        }
    571572       
     
    575576        }
    576577        })
    577         .fail(function(response) {
     578        .fail(function(response, textStatus, errorThrown) {
     579        // https://stackoverflow.com/questions/13258784/stopping-an-ajax-call-on-page-unload
     580        // https://api.jquery.com/jQuery.ajax/
     581        if(textStatus == "abort") {
     582            console.log ("INFO: Navigating away from page, so aborted running ajax.");
     583        } else {
     584       
    578585            if(errorResponseFunction != null) {
    579                 console.log("(" + callingFunction + ") Failed\n" + response);   
     586                console.log("*** (" + callingFunction + ") Failed\n" + response);   
    580587                errorResponseFunction(response);
    581588            } else {
    582589                    alert("(" + callingFunction + ") Failed\n" + response);
    583                 console.log("(" + callingFunction + ") Failed\n" + response);
     590                console.log("*** (" + callingFunction + ") Failed\n" + response);
    584591            }
     592        }
     593        })
     594        .always(function(data) { // https://api.jquery.com/jQuery.ajax/
     595
     596        // this call has terminated one way or another, remove it from the list we maintain
     597        // of runningAjaxCalls
     598        var index_of_call = runningAjaxCalls.indexOf(ajaxCall);
     599        if(index_of_call != -1) {
     600            // null this ajaxCall in the runningAjaxCalls array if not already nulled
     601            //runningAjaxCalls[index_of_call] = null;
     602
     603            // remove this 1 call from the runningAjaxCalls array
     604            runningAjaxCalls.splice(index_of_call, 1);
     605        }
     606       
    585607        });
     608
     609    // now we've launched an ajax call, let's add it to our list of runningAjaxCalls
     610    runningAjaxCalls.push(ajaxCall);
    586611    }
    587612    else {
     
    11201145    }
    11211146}
     1147
     1148// When user prematurely navigates away from current page before the page has finished all
     1149// running ajax calls, abort those still running. Else the error callback is called
     1150// and error messages are displayed.
     1151// https://stackoverflow.com/questions/13258784/stopping-an-ajax-call-on-page-unload
     1152// See also gs.functions._callMetadataServer() which maintains the array of running ajax calls
     1153// and removes finished calls from the runningAjaxCalls array.
     1154//window.onbeforeunload = function(event) {
     1155$(window).on("beforeunload", function(event) {
     1156    var i = 0;
     1157    var ajaxCall;
     1158    for (i = 0; i < runningAjaxCalls.length; i++) { // list of only *running* ajax calls
     1159    ajaxCall = runningAjaxCalls[i];
     1160    // abort each still running ajaxCall (gets removed from runningAjaxCalls list elsewhere)
     1161        ajaxCall.abort();
     1162    }
     1163});
Note: See TracChangeset for help on using the changeset viewer.