2

My problem is that, I want to perform a synchronous ajax request only when the user closes the window, not when reloading. I have a live auction manager interface, and the user should close the window only after logging out or ended the auction, because the mobile app api will mark the auction as going after the auction has ended and this gives the chance to the mobile app users to continue bidding on products. But even with the use case I sent to them, they won't log out or end the auction so I must make it fully foolproof : I send a synchronous request to end the auction. I have found the window.onbeforeunload event, but it sends the request even when I press f5 or leaving the page, which is not a good solution. The auction, the items and the bids are stored in memcache, so they won't reset after closing the window, I must delete all the items. The code so far:

window.onbeforeunload = closingCode;
function closingCode(e) {
    $.ajax({
        type: "POST",
        url: ajaxPath,
        async: false,
        data: {callback: "endOnClose", params: []},

    });
}

I hope there is a solution for this. Thanks in advance!

Pointy
  • 405,095
  • 59
  • 585
  • 614
Krisztián Dudás
  • 856
  • 10
  • 22

1 Answers1

3

I found this answer, but instead I thought of another way that might work better: what if you use ajax to poll every x second and when the polling stops on the server for 2 times that timespan execute the code then that you wanted to execute in your question.

Community
  • 1
  • 1
online Thomas
  • 8,864
  • 6
  • 44
  • 85