javascript - sweetalert 关闭后的重定向
问题描述
在 sweetalert 模式超时关闭后,我想被重定向到另一个页面。我正在使用以下代码。模式正在显示,它在计时器结束后关闭,但新页面没有被重定向。
$('#with-timer').on('click', function () {
var timerInterval;
swal({
title: 'Auto close alert!',
html: 'I will close in <strong>2</strong> seconds.',
timer: 2000
}).then(function (result) {
if (result.dismiss === swal.DismissReason.timer) {
window.location.href = "index.php";
}
});
});
有什么建议可以解决这个问题吗?
谢谢你。
解决方案
看起来您正在将 sweetalert 与 sweetalert2 混合使用。它们有不同的 API。看起来 sweetalert2 告诉您警报被解除的原因,所以我将使用它来回答这个问题,尽管它用于swal.fire
打开警报。
这是一个工作示例:
$('#with-timer').on('click', function () {
var timerInterval;
swal.fire({
title: 'Auto close alert!',
html: 'I will close in <strong>2</strong> seconds.',
timer: 2000
}).then(function (result) {
if (result.dismiss === swal.DismissReason.timer) {
window.location.href = "https://stackoverflow.com";
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<button id="with-timer">Open Modal</button>
推荐阅读
- laravel - Laravel sanctum 更改 csrf cookie 路由
- javascript - JavaScript jQuery - 复选框样式设置为电源按钮并在重新加载网页时更新其状态的问题
- flutter - 如何在颤振中调用可增长的列表
- c - 由于 SO_REUSEPORT 在 Linux 下可用,是否已弃用 SO_REUSEADDR?
- c++ - 使用 boost 库 (cpp_int) 时出现常量太大的错误
- amazon-web-services - AWS AppSync - 放大代码生成类型而不创建任何内容
- azure - 在哪里可以找到 azure 身份范围及其权限级别的列表?
- r - R 编程:“[[k]][1:nrow(my_data)]”在我的建模函数中做了什么?寻找对 R 代码的更多理解
- javascript - 如何使用其中的文本大小调整 div 宽度
- java - 如何在 javafx 中实现可调整性?