I am trying to trigger a function in my vue.js app only when the user closes the app. I already found the possibility to register an event listener for beforeunload
. Using this event, the function will also trigger when the page is just reloaded - which is not really what I want.
Can anybody tell me if there is a way to prevent beforeunload
from triggering on refresh? Or is there a completely different way to fire a function whenever my page is closed?
Thank you in advance! :)
I don't think there is a way to differentiate between a refresh and a page close, but you can maybe set a timestamp in local storage and then check that timestamp on load and if it's close you can assume it was a refresh. The before unload will still run though, but maybe you can write some js to undo whatever the before unload did when it was a refresh.
Edit: This answer to a similar question may also be helpful: https://stackoverflow.com/a/14893469/5460296