How can I update a document in Firestore on beforeunload?
function sleep() {
var start = new Date().getTime();
while (new Date().getTime() < start + 3000);
}
window.addEventListener("beforeunload", function(event) {
const ref = db.collection('users').doc(currentUser.uid);
ref.update({
profiles: true
}).then(function() {
console.log("Added!");
});
console.log("Blocking for 3 seconds...");
sleep();
console.log("Done!!");
});
Seems like ref.update() is never called? Any idea why that's the case?
question from:
https://stackoverflow.com/questions/65895509/firestore-update-document-on-beforeunload 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…