简体   繁体   中英

Timer with confirm and cancel button

i'm a noobs at sweet alert. Is it possible to create a sweet alert prompt which has confirm button, cancel and timer.

The logic is if the alert not confirmed, timer automatically execute the same function with cancel. I have tried and stuck. even if the alert confirmed, the timer still counting and the the cancel function is called.

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

Checking the isConfirm argument for null should tell you if the callback was initialized by the timer finishing.

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

As far as I could find, this behavior is not documented, but I believe this is the 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(); } ); 

the code above still execute cancelFunction(); i add a line before Confirmfunction.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM