如何在 5 秒后自动关闭对话框

时间:2021-05-28 06:42:09

标签: javascript

如何在 5 秒后关闭对话框?

function mydialog(){
  const options={
    type:'question',
    buttons:['Yes','No'],
    defaultId:[1,2],
    title:'Question',
    message:'Are You Attentive in the session?',
    detail:'Then only you will get attendence',
    checkboxLabel: 'Remember my answer',
    checkboxChecked:true,
  };
  dialog.showMessageBox(null, options, msg=>{console.log(msg); })
  console.log("dialog")
}

function loop() {
    console.log("executed")
    var rand = Math.round(Math.random() * 100) * 600;
    var c = 200
    setTimeout(function() {
        c = c + 1
        mydialog();
        
    }, rand);
    console.log({rand})
}
setInterval(loop, 60000);

1 个答案:

答案 0 :(得分:0)

看看这段代码。

other author's answer

// Make a new message box and close after n milliseconds
function tempBox(options, n){
     return new Promise(async (resolve, reject) => {
          const slave = new BrowserWindow({width: 1, height: 1, show: false});
          var resolved = false;
          const btn = dialog.showMessageBox(slave, options).then((index) => {
               resolved = true;
               resolve(index);
          }).catch(() => {});
          await new Promise((res) => setTimeout(res, n));
          slave.close();
          if (!resolved) reject();
     });
}