我试图在窗口关闭时发送数据,以防止 2 人编辑和覆盖彼此的数据。目前我在卸载事件处理程序中使用 sendBeacon。
火狐:
- 刷新:有效
- 后退按钮:工作
- 关闭窗口:作品
铬合金:
- 刷新:有效
- 后退按钮:工作
- 关闭窗口:不起作用
这是我的代码
function sendDataOnClose(edit,trans){
var url = "../../save.php"; //This has a post request handler and works properly with other functions for saving data
const data = JSON.stringify
({
"translations": trans,
"edit": edit
});
navigator.sendBeacon(url, data);
}
function handleClose(){
if(edit){
console.log("sending a false when edit is: "+ edit)
sendDataOnClose(false, translations);
}
}
window.addEventListener('unload', handleClose);