带确认和取消按钮的计时器

Timer with confirm and cancel button

我是一个甜蜜警报的菜鸟。 是否可以创建一个带有确认按钮、取消和计时器的甜蜜警报提示。

逻辑是如果警报没有被确认,定时器会自动执行与取消相同的功能。我已经尝试并卡住了。即使警报已确认,计时器仍在计时并调用取消函数。

sweetAlert({
    title: 'Stay in fullscreen',
    text: "You will be logged out in 10 seconds or if you leave the fullscreen!",
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#ff0000',
    confirmButtonText: 'Go fullscreen!',
    cancelButtonText: 'Leave this page!',
    confirmButtonClass: 'btn btn-success',
    cancelButtonClass: 'btn btn-warning',
    closeOnConfirm: 'true',
    timer:'10000',
    buttonsStyling: false
},function(isConfirm) {
    if (isConfirm) {
        return launchIntoFullscreen(document.documentElement) // timer still still counting
    } else {
        return Redirect() 
    }
}).then(function () {
    swal(
        window.alert('teest')
    )
}, function (dismiss) {
    // dismiss can be 'cancel', 'overlay',
    // 'close', and 'timer'
    if (dismiss === 'timer') {
        return Redirect()
    }
})

检查 isConfirm 参数是否为 null 应该会告诉您回调是否由计时器完成初始化。

swal({
    title: "Auto close alert!",
    text: "I will close in 2 seconds.",
    timer: 2000,
    showConfirmButton: true,
    showCancelButton: true
  }, 
  function(isConfirm) {
    if (isConfirm === null || isConfirm == false)
      cancelFunction();
    else
      confirmFunction();
  }
);

据我所知,没有记录这种行为,但我相信这是 relevant bit of the source

swal({
    title: "Auto close alert!",
    text: "I will close in 2 seconds.",
    timer: 2000,
    showConfirmButton: true,
    showCancelButton: true
  }, 
  function(isConfirm) {
    if (isConfirm === null || isConfirm == false)
      cancelFunction();
    else
      confirmFunction();
  }
);

上面的代码仍然执行cancelFunction(); 我在Confirmfunction之前加了一行。

if (isConfirm !=null && isConfirm != false) {
   closeInSeconds = Number.MAX_VALUE/1000; // this line extends timer time
   document.documentElement);
}
else {
   Redirect();
}