我正在尝试使用 javascript 创建一个计时器来运行计时器和 mysqli 绑定变量$dbSessionDuration
以从数据库中检索开始时间。问题是:
- 我收到一个未定义的变量,因为它表明我是
missing ; before statement
,但我看不到问题出在哪里,因为它与变量本身指向同一行
如何修复错误?
View source code below:
(function(){
$(document).ready(function() {
var time =
Notice: Undefined variable: dbSessionDurationTime in /web/stud/u0867587/Mobile_app/assessment.php on line 108
null,
parts = time.split(':'),
hours = +parts[0],
minutes = +parts[1],
seconds = +parts[2],
span = $('#countdown');
function correctNum(num) {
return (num<10)? ("0"+num):num;
}
var timer = setInterval(function(){
seconds--;
if(seconds == -1) {
seconds = 59;
minutes--;
if(minutes == -1) {
minutes = 59;
hours--;
if(hours==-1) {
alert("timer finished");
clearInterval(timer);
return;
}
}
}
span.text(correctNum(hours) + ":" + correctNum(minutes) + ":" + correctNum(seconds));
}, 1000);
});
});
实际代码如下:
(function(){
$(document).ready(function() {
var time = <?php echo json_encode($dbSessionDurationTime); ?>,
parts = time.split(':'),
hours = +parts[0],
minutes = +parts[1],
seconds = +parts[2],
span = $('#countdown');
function correctNum(num) {
return (num<10)? ("0"+num):num;
}
var timer = setInterval(function(){
seconds--;
if(seconds == -1) {
seconds = 59;
minutes--;
if(minutes == -1) {
minutes = 59;
hours--;
if(hours==-1) {
alert("timer finished");
clearInterval(timer);
return;
}
}
}
span.text(correctNum(hours) + ":" + correctNum(minutes) + ":" + correctNum(seconds));
}, 1000);
});
});