0

我正在尝试使用 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);
  }); 

});
4

3 回答 3

2

这实际上是 PHP 问题而不是 javascript 问题。您的变量$dbSessionDurationTime未定义。

于 2013-02-06T22:49:49.270 回答
1

$dbSessionDurationTime没有定义。如果你去定义你不应该再收到这个错误。这是 PHP 而不是 Java 脚本。

于 2013-02-06T22:50:59.483 回答
1

变量 $dbSessionDuration 未定义。来自 Javascript 代码中间的 PHP 代码的错误消息导致语法错误。

于 2013-02-06T22:52:24.340 回答