【发布时间】:2020-11-09 04:51:02
【问题描述】:
我正在编写一个电子应用程序,如果用户打开了未保存的文件,我想在保存之前提示用户。我在网上找到了这个示例代码:
window.onbeforeunload = (e) => {
var answer = confirm('Do you really want to close the application?');
e.returnValue = answer; // this will *prevent* the closing no matter what value is passed
if(answer) { mainWindow.destroy(); } // this will close the app
};
如果在出现后几秒钟内按下“是”、“取消”或“X”按钮,则此代码会奇怪地工作,但是如果您让对话框在屏幕上停留片刻,然后单击按钮,则无论按下什么,应用程序都会关闭.
此代码位于 index.html 调用的我的主脚本文件中
【问题讨论】:
标签: javascript electron