我已经在这一段时间了很长一段时间但似乎无法解决为什么这个javascript计时器,使用PHP变量实际上不会倒计时...
这是我的代码
<script>
<?php
if($_SESSION['mydropdown'] == 1){
$var = ($_SESSION['timer'] + 0.1 * 60) - time();
}
else {
$var = ($_SESSION['timer'] + 1440 * 60) - time();
}
?>
setInterval(function() {
var difference = Math.floor('<?php echo $var ;?>');
var seconds = fixIntegers(difference % 60);
difference = Math.floor(difference / 60);
var minutes = fixIntegers(difference % 60);
difference = Math.floor(difference / 60);
var hours = fixIntegers(difference % 24);
difference = Math.floor(difference / 24);
var days = difference;
$("#Timer").text(seconds + " Seconds"+ minutes + "m" + hours + "h" + days + "d" );
}, 1000);
</script>
You will be logged out in : <span id="Timer"></span>
基本上,如果dropdown
为1,则第一个计时器应为6秒,但它是静止的6秒并且根本不会倒计时...我直接从另一个正常倒计时的功能,但我似乎无法找出这个问题出在哪里,以及为什么它不会真正倒计时。基本上我希望它从6到5到4,依此类推到0,如果它是6秒。
我安装了jquery / jscript,因为我的其他计时器正在运行,它不包含php变量。
提前致谢
答案 0 :(得分:1)
将此var difference = Math.floor(<?php echo $var; ?>);
语句放在setInterval
调用的匿名函数之外。而且您不需要'
单引号
你的代码应该是这样的:
var difference = Math.floor('<?php echo $var ;?>');
setInterval(function() {
var d = difference; // This line is added
var seconds = fixIntegers(difference % 60);
difference = Math.floor(difference / 60);
var minutes = fixIntegers(difference % 60);
difference = Math.floor(difference / 60);
var hours = fixIntegers(difference % 24);
difference = Math.floor(difference / 24);
var days = difference;
difference = d - 1; // This line is added
$("#Timer").text(seconds + " Seconds"+ minutes + "m" + hours + "h" + days + "d" );
}, 1000);