如何解决如何防止创建多个 ELECTRON 实例 dialog.showMessageBox
问题是每次调用对话框函数时都会创建多个对话框,第一个只创建一个,第二个调用创建两个,第三个调用创建三个实例,依此类推....更像是选择一个选项后,之前的对话框不会关闭,而是隐藏自己,并且所有对话框都会在下一个对话框调用中再次显示。
用法
如果用户尝试获取外部数据但互联网不可用,我将使用该对话框向用户显示一条消息。
谢谢
const { ipcMain } = require("electron");
let isOnline = (win,once = false,pass,user_callback) => {
// the availability of internet flag
let CONNECTION;
// flags to indicate if it's the first check to not execute multiple functions
let FIRSTCH = true;
// flags to execute the fetching only once
let DONE = false;
/**
* Show a warning to the user.
* You can retry in the dialog until a internet connection
* is active.
*/
ipcMain.on("online-status-changed",(event,status) => {
if (status === "online") {
CONNECTION = true;
// Verify for first time
if (FIRSTCH) {
if (!DONE) {
execute();
FIRSTCH = false;
}
}
} else {
if (!DONE) {
FIRSTCH = false;
CONNECTION = false;
execute();
}
}
});
var message = function () {
const { dialog } = require("electron");
dialog
.showMessageBox(win,{
title: "There's no internet",message: "No internet available,do you want to try again ?",type: "warning",buttons: ["Try Again","Enter Without Fetching Data"],defaultId: 0,})
.then((result) => {
if (result.response === 0) {
// try again
execute();
}
if (result.response === 1) {
//enter without fetch
pass();
}
});
};
var execute = function () {
if (CONNECTION) {
// Execute action if internet available.
user_callback();
if (once) DONE = true;
} else {
// Show warning to user
// And "retry" to connect
message();
}
};
};
module.exports = isOnline;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。