Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make an async call in Javascript onunload element?

I have a system where items are locked (with a flag in the database) when a user is vieiwing that item.

Currently the item is only unlocked when a user performs a certain action.

However, when a user leaves the page through any method, I'd like to make a call to a webservice / ashx page that will unlock the item, but not stop the page from changing.

like image 789
cjk Avatar asked Mar 13 '26 13:03

cjk


1 Answers

What about using a "heartbeat" function to keep the lock alive while the page is loaded? Make a server call every, say, 15 seconds or so to renew the lock. Program your server-side code so that a lock will time out after 20 seconds if it hasn't heard the "heartbeat" from the page. When the user navigates away from the page, the heartbeat will stop, and the lock will be released a maximum of 20 seconds later.

like image 195
Joshua Carmody Avatar answered Mar 16 '26 01:03

Joshua Carmody



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!