0

当我加载页面时,无论我做什么都不会出现时钟。我创建了两个按钮。如果我删除引用stopTime()包含的功能的按钮,clearTimeout(t);则会出现我的时钟。如果我把按钮放回去,我的时钟将无法工作。

<body onload="getTime()">


  <script>

  var t;

  function getTime() 
  {  
     var time = new Date();

     var hour = time.getHours();
     var minute = time.getMinutes();
     var second = time.getSeconds(); 

     hour = checkTime(hour);
     minute = checkTime(minute);
     second = checkTime(second);

     var actualtime = hour + ":" + minute + ":" + second;

     document.getElementById("time").innerHTML = actualtime;

     t = setTimeout(function(){getTime()},1000);
  }

  function checkTime(i)
  {
      if ( i < 10 )
          i = "0" + i;
      return i;
  }

  function stopTime();
  {
      clearTimeout(t);
  }

  </script>


</body>

</html>

提前致谢!

4

2 回答 2

4

你在这里有一个语法错误:

function stopTime();
{
    clearTimeout(t);
}

取出. ;_function stopTime()

解决这个问题,您的代码对我来说运行良好。

于 2012-09-17T22:46:25.740 回答
0

那里有 javascripts 错误。修复了js错误尝试下面的代码

var t;
function checkTime(i) {
    if (i < 10){ i = "0" + i;}
    return i;
}

function stopTime(){
    clearTimeout(t);
}



function getTime() {
    var time = new Date();

    var hour = time.getHours();
    var minute = time.getMinutes();
    var second = time.getSeconds();

    hour = checkTime(hour);
    minute = checkTime(minute);
    second = checkTime(second);

    var actualtime = hour + ":" + minute + ":" + second;

    document.getElementById("time").innerHTML = actualtime;

    t = setTimeout(function() {
        getTime();
    }, 1000);
}
于 2012-09-17T22:58:05.537 回答