首页 > 解决方案 > Sweetalert延迟弹出窗口

问题描述

我正在尝试延迟 SweetAlert 弹出窗口,使其在触发后几秒钟内不显示。

例如,用户在网页上执行操作以触发 SweetAlert,但不是立即显示,而是等待 2 秒然后显示。- 我一直在研究各种方法来做到这一点,但没有运气......我认为可能setTimeout需要?

这是我的 SweetAlert 函数,它目前正在工作:

if( response == 10 ){
    swal({
      type: 'success',
      title: 'YOUR BALANCED BOX IS FULL',
      text: 'Youve added the recommended amount of items for your plan!',
      allowOutsideClick: false,
      showCancelButton: true,
      cancelButtonText: "Modify Selection",
      cancelButtonColor: '#d33',
      showConfirmButton: true,
      confirmButtonColor: '#61ce70',
      confirmButtonText: "Proceed To Cart",
    }).then((result) => {
        if (result.value) {
            window.location = "/basket/";
        }
    }) 
}

任何帮助表示赞赏!

标签: javascriptalertsweetalertsweetalert2

解决方案


具体的 SWEETALERT 2 答案:

上面来自 Leo 的代码逻辑答案是正确的。我与 SweetAlert 2 共享最终版本,并setTimeout添加了一项功能以延迟弹出窗口的打开。

The setTimeout function wraps around the entire SweetAlert 2 function and the timer is set at the bottom of the end of the function (in this case to 3000).

Hope this helps anyone looking to do the same!

    setTimeout(function(){swal({
  type: 'success',
  title: 'YOUR BALANCED BOX IS FULL',
  text: 'Youve added the recommended amount of items for your plan!',
  allowOutsideClick: false,
  showCancelButton: true,
  cancelButtonText: "Modify Selection",
  cancelButtonColor: '#d33',
  showConfirmButton: true,
  confirmButtonColor: '#61ce70',
  confirmButtonText: "Proceed To Cart",
}).then((result) => {
  if (result.value) {
    window.location = "/basket/";
  }
})},3000); 

推荐阅读