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

时间:2017-03-08 02:34:41

标签: javascript jquery sweetalert

我是甜蜜警报的新手。 是否可以创建一个确认按钮,取消和计时器的甜蜜警报提示。

逻辑是如果警报未确认,则计时器自动执行与取消相同的功能。我试过并坚持下去。即使警报确认,计时器仍在计数,并且调用取消功能。

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()
    }
})

2 个答案:

答案 0 :(得分:0)

检查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

答案 1 :(得分:0)

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(); 我在确认功能之前添加了一行。

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