1

如果用户未登录,我试图在登录时自动重定向之前设置 sweetalert2,但它只出现一秒钟并立即开始重定向,我如何设置等待用户点击?

我的实际代码:

<?php
session_start();
if ($_SESSION[logged] != 'yes') { 
echo '
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<link rel="stylesheet" href="stylesheets/sweetalert2.css">
<script src="javascripts/custom/sweetalert2.min.js"></script>
<script>
$( document ).ready(function() {
    swal("Oops...", "Not logged!", "error");
    document.location.href="/login.html"
});
</script>';
exit; 
}
?>
4

3 回答 3

0

您可以添加一个计时器,例如这个,等待 5 秒,然后使用重定向

  $( document ).ready(function() {
    alert("Oops...Not logged! error");
    var millisecondsToWait = 5000;
    setTimeout(function() {
       document.location.href="/login.html"
    }, millisecondsToWait);

  });

而不是警报,只需使用 swal。

于 2015-09-08T13:17:10.003 回答
0

你需要的是延迟重定向线

   setTimeout(function () {
       window.location.href = "/login.html"; //will redirect to your page after 2000 ms
    }, 2000);

来源: https ://stackoverflow.com/a/9877274/3615630

于 2015-09-08T13:34:42.987 回答
0

Sweet alert 有回调函数:

swal({
    //options
}, function(isConfirm) {
     document.location.href="/login.html"
});
于 2015-09-08T13:17:26.903 回答