Execute a call during an onbeforeunload event










2















I need to send a DELETE request to unlock the item used in the page when the browser is being closed; to do so, i've used onbeforeunload (the browsers supported by specs are only Chrome and Firefox).



If i use a return on the method, it show a pop-up message and the call work fine.



But, unluckily, the specs required to avoid this pop-up message; in this case, when the browser is closed, sometimes it works and sometimes it won't.



More specifically, i've got these cases:



  1. Single window with a locked item.

  2. More windows of the same browser with a locked item in one of these and the same item only displayed in the others.

  3. One windows of a browser whit the locked item and one or more windows of the other browser with the same item displayed.

When i close the window with the locked item the call unlock it, so if i refresh one of the pages where the same item was displayed it result utilizable.



This is what happen with the current "solution":



Firefox:
- Case 1: doesn't unlock the item.
- Case 2: unlock the item.
- Case 3: doesn't unlock the item.

Chrome:
- Case 1: doesn't unlock the item.
- Case 2: unlock the item.
- Case 3: unlock the item.


The current implementation is:



$window.onbeforeunload = function()
MyService.delete(id: item.id); //The service is defined on another .js



Other possible solution that does't worked:



1) using onunload or onclose in place of onbeforeunload



2) whit this answer https://stackoverflow.com/a/26275621/8879273 i managed to intercept the keys event, but if the window is closed with the mouse it doesn't work (also, every time i move the cursor on the item it starts a delete call)



i've tried to change the mouse trigger with



$(window).on('click', (function () 
window.onbeforeunload = MyService.delete( id: item.id );
));


and



$(window).on('mousedown', (function () 
window.onbeforeunload = MyService.delete( id: item.id );
));


but it still don't work



3) using defer



$window.onbeforeunload = function (event) 
$scope.closing();


$scope.closing = function()
var deferred = $q.defer();
MyService.delete( id: item.id ).then(function(response)
deferred.resolved(response);
, function(error)
deferred.resolved(error);
);
return deferred.promise;



4) take by desperation, i've writed this (don't try it: it works fine... if you want to lock the whole computer!)



$scope.unlocking = false;

$window.onbeforeunload = function()
MyService.delete(id: item.id).then(function(response)
$scope.unlocking = true;
, function(error)
$scope.unlocking = true; // it doesn't matter if is a successful response or not
);

// since call are async, i've thinked to put a while to wait the response

var res = angular.copy($scope.unlocking);
while(res)
$timeout(function()
res = angular.copy($scope.unlocking);
, 100);




the problem whit this code is that the response return but the while doesn' exit.










share|improve this question






















  • I suggest a completely different approach: use socket.io / websockets to keep a connection between client and server, and if the connection is interrupted, unlock the item.

    – Chris G
    Nov 15 '18 at 12:08















2















I need to send a DELETE request to unlock the item used in the page when the browser is being closed; to do so, i've used onbeforeunload (the browsers supported by specs are only Chrome and Firefox).



If i use a return on the method, it show a pop-up message and the call work fine.



But, unluckily, the specs required to avoid this pop-up message; in this case, when the browser is closed, sometimes it works and sometimes it won't.



More specifically, i've got these cases:



  1. Single window with a locked item.

  2. More windows of the same browser with a locked item in one of these and the same item only displayed in the others.

  3. One windows of a browser whit the locked item and one or more windows of the other browser with the same item displayed.

When i close the window with the locked item the call unlock it, so if i refresh one of the pages where the same item was displayed it result utilizable.



This is what happen with the current "solution":



Firefox:
- Case 1: doesn't unlock the item.
- Case 2: unlock the item.
- Case 3: doesn't unlock the item.

Chrome:
- Case 1: doesn't unlock the item.
- Case 2: unlock the item.
- Case 3: unlock the item.


The current implementation is:



$window.onbeforeunload = function()
MyService.delete(id: item.id); //The service is defined on another .js



Other possible solution that does't worked:



1) using onunload or onclose in place of onbeforeunload



2) whit this answer https://stackoverflow.com/a/26275621/8879273 i managed to intercept the keys event, but if the window is closed with the mouse it doesn't work (also, every time i move the cursor on the item it starts a delete call)



i've tried to change the mouse trigger with



$(window).on('click', (function () 
window.onbeforeunload = MyService.delete( id: item.id );
));


and



$(window).on('mousedown', (function () 
window.onbeforeunload = MyService.delete( id: item.id );
));


but it still don't work



3) using defer



$window.onbeforeunload = function (event) 
$scope.closing();


$scope.closing = function()
var deferred = $q.defer();
MyService.delete( id: item.id ).then(function(response)
deferred.resolved(response);
, function(error)
deferred.resolved(error);
);
return deferred.promise;



4) take by desperation, i've writed this (don't try it: it works fine... if you want to lock the whole computer!)



$scope.unlocking = false;

$window.onbeforeunload = function()
MyService.delete(id: item.id).then(function(response)
$scope.unlocking = true;
, function(error)
$scope.unlocking = true; // it doesn't matter if is a successful response or not
);

// since call are async, i've thinked to put a while to wait the response

var res = angular.copy($scope.unlocking);
while(res)
$timeout(function()
res = angular.copy($scope.unlocking);
, 100);




the problem whit this code is that the response return but the while doesn' exit.










share|improve this question






















  • I suggest a completely different approach: use socket.io / websockets to keep a connection between client and server, and if the connection is interrupted, unlock the item.

    – Chris G
    Nov 15 '18 at 12:08













2












2








2








I need to send a DELETE request to unlock the item used in the page when the browser is being closed; to do so, i've used onbeforeunload (the browsers supported by specs are only Chrome and Firefox).



If i use a return on the method, it show a pop-up message and the call work fine.



But, unluckily, the specs required to avoid this pop-up message; in this case, when the browser is closed, sometimes it works and sometimes it won't.



More specifically, i've got these cases:



  1. Single window with a locked item.

  2. More windows of the same browser with a locked item in one of these and the same item only displayed in the others.

  3. One windows of a browser whit the locked item and one or more windows of the other browser with the same item displayed.

When i close the window with the locked item the call unlock it, so if i refresh one of the pages where the same item was displayed it result utilizable.



This is what happen with the current "solution":



Firefox:
- Case 1: doesn't unlock the item.
- Case 2: unlock the item.
- Case 3: doesn't unlock the item.

Chrome:
- Case 1: doesn't unlock the item.
- Case 2: unlock the item.
- Case 3: unlock the item.


The current implementation is:



$window.onbeforeunload = function()
MyService.delete(id: item.id); //The service is defined on another .js



Other possible solution that does't worked:



1) using onunload or onclose in place of onbeforeunload



2) whit this answer https://stackoverflow.com/a/26275621/8879273 i managed to intercept the keys event, but if the window is closed with the mouse it doesn't work (also, every time i move the cursor on the item it starts a delete call)



i've tried to change the mouse trigger with



$(window).on('click', (function () 
window.onbeforeunload = MyService.delete( id: item.id );
));


and



$(window).on('mousedown', (function () 
window.onbeforeunload = MyService.delete( id: item.id );
));


but it still don't work



3) using defer



$window.onbeforeunload = function (event) 
$scope.closing();


$scope.closing = function()
var deferred = $q.defer();
MyService.delete( id: item.id ).then(function(response)
deferred.resolved(response);
, function(error)
deferred.resolved(error);
);
return deferred.promise;



4) take by desperation, i've writed this (don't try it: it works fine... if you want to lock the whole computer!)



$scope.unlocking = false;

$window.onbeforeunload = function()
MyService.delete(id: item.id).then(function(response)
$scope.unlocking = true;
, function(error)
$scope.unlocking = true; // it doesn't matter if is a successful response or not
);

// since call are async, i've thinked to put a while to wait the response

var res = angular.copy($scope.unlocking);
while(res)
$timeout(function()
res = angular.copy($scope.unlocking);
, 100);




the problem whit this code is that the response return but the while doesn' exit.










share|improve this question














I need to send a DELETE request to unlock the item used in the page when the browser is being closed; to do so, i've used onbeforeunload (the browsers supported by specs are only Chrome and Firefox).



If i use a return on the method, it show a pop-up message and the call work fine.



But, unluckily, the specs required to avoid this pop-up message; in this case, when the browser is closed, sometimes it works and sometimes it won't.



More specifically, i've got these cases:



  1. Single window with a locked item.

  2. More windows of the same browser with a locked item in one of these and the same item only displayed in the others.

  3. One windows of a browser whit the locked item and one or more windows of the other browser with the same item displayed.

When i close the window with the locked item the call unlock it, so if i refresh one of the pages where the same item was displayed it result utilizable.



This is what happen with the current "solution":



Firefox:
- Case 1: doesn't unlock the item.
- Case 2: unlock the item.
- Case 3: doesn't unlock the item.

Chrome:
- Case 1: doesn't unlock the item.
- Case 2: unlock the item.
- Case 3: unlock the item.


The current implementation is:



$window.onbeforeunload = function()
MyService.delete(id: item.id); //The service is defined on another .js



Other possible solution that does't worked:



1) using onunload or onclose in place of onbeforeunload



2) whit this answer https://stackoverflow.com/a/26275621/8879273 i managed to intercept the keys event, but if the window is closed with the mouse it doesn't work (also, every time i move the cursor on the item it starts a delete call)



i've tried to change the mouse trigger with



$(window).on('click', (function () 
window.onbeforeunload = MyService.delete( id: item.id );
));


and



$(window).on('mousedown', (function () 
window.onbeforeunload = MyService.delete( id: item.id );
));


but it still don't work



3) using defer



$window.onbeforeunload = function (event) 
$scope.closing();


$scope.closing = function()
var deferred = $q.defer();
MyService.delete( id: item.id ).then(function(response)
deferred.resolved(response);
, function(error)
deferred.resolved(error);
);
return deferred.promise;



4) take by desperation, i've writed this (don't try it: it works fine... if you want to lock the whole computer!)



$scope.unlocking = false;

$window.onbeforeunload = function()
MyService.delete(id: item.id).then(function(response)
$scope.unlocking = true;
, function(error)
$scope.unlocking = true; // it doesn't matter if is a successful response or not
);

// since call are async, i've thinked to put a while to wait the response

var res = angular.copy($scope.unlocking);
while(res)
$timeout(function()
res = angular.copy($scope.unlocking);
, 100);




the problem whit this code is that the response return but the while doesn' exit.







javascript session browser






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 12:01









P. VucinicP. Vucinic

213




213












  • I suggest a completely different approach: use socket.io / websockets to keep a connection between client and server, and if the connection is interrupted, unlock the item.

    – Chris G
    Nov 15 '18 at 12:08

















  • I suggest a completely different approach: use socket.io / websockets to keep a connection between client and server, and if the connection is interrupted, unlock the item.

    – Chris G
    Nov 15 '18 at 12:08
















I suggest a completely different approach: use socket.io / websockets to keep a connection between client and server, and if the connection is interrupted, unlock the item.

– Chris G
Nov 15 '18 at 12:08





I suggest a completely different approach: use socket.io / websockets to keep a connection between client and server, and if the connection is interrupted, unlock the item.

– Chris G
Nov 15 '18 at 12:08












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319081%2fexecute-a-call-during-an-onbeforeunload-event%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319081%2fexecute-a-call-during-an-onbeforeunload-event%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3