我想创建一个脚本,在用户操作后开始4小时倒计时。
例如,假设用户可以选择购买武器,然后可以在4小时后再次购买。因此,当用户购买武器时,将开始4小时倒计时,并且在4小时过后,用户可以再次购买武器。
我已经为特定的日期和时间做了一个倒计时脚本,但我不知道该怎么做。
答案 0 :(得分:0)
好的,这就是客户端代码
var buy = function( weaponID ){
//Ajax call to buy.php
//lets say you post one variable containing the id of the weapon
$.ajax({
type: 'POST',
url: 'buy.php',
data: "id="+weaponID,
success: function(data){
if(data==1) updateScreen()
else alert("Please wait X more time")
}
})
}
服务器端代码
buy.php:
$weaponID = $_POST["id"];
$userID = $_SESSION["userID"];
$q = mysql_query(" SELECT time,
weaponCount
FROM table
WHERE user = $userID
;");
$e = mysql_fetch_array($q);
list( $time, $wcount ) = $e;
$wcount++;
$currTime = time();
$diff = $currTime - $time;
$hours = $diff / ( 60 * 60 );
if( $hours > 4 ){
mysql_query(" UPDATE table
SET time = $currTime,
weaponCount = $wcount
WHERE user = $userID
;");
echo 1;
}
else echo 0;