Sweetalert 延迟弹出窗口打开

Sweetalert delay popup from opening

我正在尝试延迟 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/";
        }
    }) 
}

感谢任何帮助!

是的,您确实可以使用 setTimeout 轻松实现此目的,我设置了一个简单的片段,您可以尝试一下!

// When button gets clicked.
$("#button").click(() => {
 // Instantely change it's text to loading..
 $("#button").text("Loading!");
  // Wait 2kms then change the text to Done!
 setTimeout(() => {
   $("#button").text("Done!");
  }, 2000);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="button">Event</button>

-狮子座

具体甜食 2 答案:

Leo 的上述代码逻辑答案是正确的。我与 SweetAlert 2 共享最终版本 setTimeout 作为添加的功能以延迟打开弹出窗口。

setTimeout 函数环绕整个 SweetAlert 2 函数,定时器设置在函数末尾的底部(在本例中为 3000)。

希望这对任何想做同样事情的人有所帮助!

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