看不到我错过的地方;在声明之前

时间:2013-02-06 22:47:00

标签: jquery

Iam尝试使用javascript创建一个计时器来运行计时器和mysqli绑定变量$dbSessionDuration以从db检索开始时间。问题是:

  • 我收到一个未定义的变量,因为它声明我是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);
  }); 

});

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

$dbSessionDurationTime未定义。如果您去定义您不应再收到此错误。这是PHP而不是Java Script。

答案 2 :(得分:1)

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