Synchronous AJAX recording from unload event: how to ensure that the user sees the latest information from the database when loading the next page

When a user requests editing an entry in our CMS, we “lock” it so that no one else can edit it at the same time, we release the lock when they submit their changes. However, we need to handle the case when the user leaves the page through other links ... My first attempt is to use jQuery to start a synchronous call $.ajax()to $(window).unload. This works, however, on the next page that the user sees, the entry appears to be locked (provided that they return to the page with a list of all entries, perhaps using the "Back" button). A simple update of this page shows that the record is ready for editing again.

I assume that for some reason the browser will receive the next page before the ajax request is fully processed. The question is, is there a way to ensure that things happen in the correct order.

var fire_unload_ajax = true; // certain things set this to false, not relevant
$(window).unload(function() {
    if(fire_unload_ajax && $("#reset-entry-lock form").length == 1) {
        $.ajax({
            url: window.location.href,
            async: false,
            cache: false,
            type: "POST",
            data: $("#reset-entry-lock form").serialize()
        });
    }
});
+3
source share
2 answers

I solved a similar problem in an application I wrote recently.

I ended up with the code in the load event of every page of my application, which was checked to make sure the user still has locks and are released if they are no longer required. I also had a code, similar to yours, that issued locks during the unload event, in case the user went to a page outside my application.

Opera ( ..) , , 30 , . , , , , . , , .

+2

AJAX POST , Gmail . , onbeforeunload.

0

Source: https://habr.com/ru/post/1732066/


All Articles