首页 > 解决方案 > 当 setimeout == 0 时更改页面

问题描述

我有这个功能,我想要时间等于 0。我想让我的页面从 index.php 更改为 gameover.html。

function progress(timeleft, timetotal, $element) {
    var progressBarWidth = timeleft * $element.width() / timetotal;
    $element
        .find('div')
        .animate({ width: progressBarWidth }, 500)
        .html(timeleft + " seconds to go");
    if(timeleft > 0) {
        setTimeout(function() {
            progress(timeleft - 1, timetotal, $element);
        }, 1000);
    }
};

progress(180, 180, $('#progressBar'));

我想,到那个 if(timeleft==0) {//i use much things but i didn't managed }

我在 index.php 上的 php 代码,所以我将能够连接到我的数据库

session_start();
include("s/connect.php");

if($_SERVER['REQUEST_METHOD'] == "POST")
{
    
    //something was posted
    $user_name = $_POST['user_name'];
    $password = $_POST['password'];
    if(!empty($user_name) && !empty($password) && !is_numeric($user_name))
    {
        
    $user_id = random_num(20);  
        $query = "insert into users ( user_id,user_name,password) values ('$user_id','$user_name','$password')";
    
    mysqli_query($mysqli, $query);
    header("Location: gameover.html");
    die;
    }
    else{
        echo "Please enter some valid information!";
    }
    
}

标签: javascriptphphtmljquery

解决方案


试试这个(如果 timeleft 不 >0,做一个重定向)

<script>
function progress(timeleft, timetotal, $element) {
    var progressBarWidth = timeleft * $element.width() / timetotal;
    $element
        .find('div')
        .animate({ width: progressBarWidth }, 500)
        .html(timeleft + " seconds to go");
    if(timeleft > 0) {
        setTimeout(function() {
            progress(timeleft - 1, timetotal, $element);
        }, 1000);
    }else{
    window.location.href='gameover.html';
    }
};

progress(180, 180, $('#progressBar'));


</script>

推荐阅读