javascript - Sweetalert 延迟弹出窗口打开

标签 javascript alert sweetalert sweetalert2

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

感谢任何帮助!

最佳答案

特定的 SWEETALERT 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); 

关于javascript - Sweetalert 延迟弹出窗口打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51794373/

相关文章:

javascript - 切换内容链接

javascript - 如何使用 Javascript 读取 CSS 属性值?

php suhosin 警报

vue.js - Vue 以编程方式选择关闭下拉菜单

javascript - Reactjs Nodejs通过axios上传ftp文件

javascript - 使用类过滤多行并计算行以更改进度条

javascript - 合并仅使用代码的 CSS 和 JS 文件

javascript - 覆盖 window.alert 并传递多个参数

javascript - 如何使用 sweetAlert js 重定向 chice 上的警报按钮?

javascript - SweetAlert,确认后聚焦输入