Javascript 当setimeout==0时更改页面
我有这个函数,我希望时间等于0。我希望我的页面从index.php更改为gameover.htmlJavascript 当setimeout==0时更改页面,javascript,php,html,jquery,Javascript,Php,Html,Jquery,我有这个函数,我希望时间等于0。我希望我的页面从index.php更改为gameover.html function progress(timeleft, timetotal, $element) { var progressBarWidth = timeleft * $element.width() / timetotal; $element .find('div') .animate({ width: progressBarWidth }, 5
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'));
我想,对于这个如果(timeleft==0){//我使用了很多东西,但是我没有管理}
我在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!";
}
}
尝试此操作(如果timeleft不>0,请执行重定向)
功能进度(timeleft,timetotal,$element){
var progressBarWidth=timeleft*$element.width()/timetotal;
$element
.find('div'))
.animate({width:progressBarWidth},500)
.html(timeleft+“还剩几秒”);
如果(时间间隔>0){
setTimeout(函数(){
进度(timeleft-1,timetotal$元素);
}, 1000);
}否则{
window.location.href='gameover.html';
}
};
进度(180,180,$);
不,现在进度条在0之前不会移动。哦,然后尝试将window.location.href=“gameover.html”更改为window.location.href=“gameover.html”这是180秒,我们等着看:谢谢大家:)不客气。享受编码的乐趣!警告:您完全可以使用参数化的预处理语句,而不是手动生成查询。它们由或提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行。
<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>