Saving data in localstorage upon close?

Viewed 134

I am trying to get values saved in localstorage so that they can persist when the user comes back to the site. Here is what I have:

function saveConfirmation() {
    if (confirm("Are you sure you want to exit? Progress will be saved.")) {
        localStorage.setItem("count", String(count))
    }
}
function saveBeforeUnload() {
    setTimeout(saveConfirmation, 0)
    return "You have unsaved changes"
}

window.onbeforeunload = saveBeforeUnload

When I click out of the tab, Chrome gives a popup asking if I want to leave the page. When I say yes, my other dialogue comes up right as it closes so I can't interact with it. If I say no, my other dialogue comes up and doesn't close when I confirm. How do I get it to where it either 1) Saves when I confirm the popup from Chrome or 2) Get rid of the Chrome popup and save and exit when I confirm my popup?

0 Answers
Related