Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的clearInterval函数有问题 Javascript中的实时时钟_Javascript_Html_Web_Clock_Clearinterval - Fatal编程技术网

我的clearInterval函数有问题 Javascript中的实时时钟

我的clearInterval函数有问题 Javascript中的实时时钟,javascript,html,web,clock,clearinterval,Javascript,Html,Web,Clock,Clearinterval,根据您的电脑,时间为 秒表 启动时钟 我有很多HTML,我用它们来生成一个实时时钟。我的任务是创建两个按钮,一个停止时钟,另一个重新启动时钟。我的setInterval函数工作得很好,但我一辈子都无法找出clearInterval函数不工作的原因。有什么想法吗 干杯您需要保存从setInterval返回的值,并将其传递到调用clearInterval中 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt

根据您的电脑,时间为


秒表 启动时钟 我有很多HTML,我用它们来生成一个实时时钟。我的任务是创建两个按钮,一个停止时钟,另一个重新启动时钟。我的setInterval函数工作得很好,但我一辈子都无法找出clearInterval函数不工作的原因。有什么想法吗


干杯

您需要保存从
setInterval
返回的值,并将其传递到调用
clearInterval

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
<!--

function init()
{
  timeDisplay = document.createTextNode ( "" );
  document.getElementById("clock").appendChild ( timeDisplay );
}

function updateClock()
{
  var currentTime = new Date ();

  var currentHours = currentTime.getHours();
  var currentMinutes = currentTime.getMinutes();
  var currentSeconds = currentTime.getSeconds();

  // Adds zeros if required
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

  // Decides whether AM or PM
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

  // Convert the hours component to 12-hour format if needed
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

  // Convert an hours component of "0" to "12"
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;

  // Creates the display string
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

  // Updates the time display
  document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}

// -->
</script>
<link rel="stylesheet" type="text/css" href="week9live.css" />
</head>

<body onload="updateClock(); setInterval('updateClock()', 1000 )">
<h1>A live clock in Javascript</h1>
<div>
  <p>The time according to your pc is </p> <span id="clock">&nbsp;</span>
</div>
</br>
<button type ="button" onclick = "clearInterval('updateClock()')">Stop Clock</button>
<button type="button" onclick="setInterval('updateClock()', 1000)">Start Clock</button>
</body>
</html>
然后

var idForClear = setInterval(updateClock, 1000);
如果您放弃内联dom级别0处理程序并执行类似的操作,这将容易得多

clearInterval(idForClear);
您希望传入一个函数

setInterval('updateClock()', 1000);
当然,UpdateLock也是一个函数,因此上面的代码与

setInterval(function() { updateClock(); }, 1000);

您需要保存从
setInterval
返回的值,并将其传递到调用
clearInterval

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
<!--

function init()
{
  timeDisplay = document.createTextNode ( "" );
  document.getElementById("clock").appendChild ( timeDisplay );
}

function updateClock()
{
  var currentTime = new Date ();

  var currentHours = currentTime.getHours();
  var currentMinutes = currentTime.getMinutes();
  var currentSeconds = currentTime.getSeconds();

  // Adds zeros if required
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

  // Decides whether AM or PM
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

  // Convert the hours component to 12-hour format if needed
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

  // Convert an hours component of "0" to "12"
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;

  // Creates the display string
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

  // Updates the time display
  document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}

// -->
</script>
<link rel="stylesheet" type="text/css" href="week9live.css" />
</head>

<body onload="updateClock(); setInterval('updateClock()', 1000 )">
<h1>A live clock in Javascript</h1>
<div>
  <p>The time according to your pc is </p> <span id="clock">&nbsp;</span>
</div>
</br>
<button type ="button" onclick = "clearInterval('updateClock()')">Stop Clock</button>
<button type="button" onclick="setInterval('updateClock()', 1000)">Start Clock</button>
</body>
</html>
然后

var idForClear = setInterval(updateClock, 1000);
如果您放弃内联dom级别0处理程序并执行类似的操作,这将容易得多

clearInterval(idForClear);
您希望传入一个函数

setInterval('updateClock()', 1000);
当然,UpdateLock也是一个函数,因此上面的代码与

setInterval(function() { updateClock(); }, 1000);

clearInterval
函数应接收由
setInterval
生成的id。比如:

setInterval(updateClock, 1000);

clearInterval
函数应接收由
setInterval
生成的id。比如:

setInterval(updateClock, 1000);

clearInterval中setInterval的传递id
请参阅:

在clearInterval中传递setInterval的id
请参阅:

clearInterval
需要由
setInterval
返回的ID。阅读一些文档而不是假设事情是如何工作的总是值得的:
clearInterval
需要一个由
setInterval
返回的ID。阅读一些文档而不是假设事情是如何运作的总是值得的:谢谢你的帮助,伙计。我已经将您提供的代码添加到页面正文的底部,但是按钮似乎仍然不起作用。我的页面中的哪些代码需要删除或更改?在Javascript方面并非最热门:P@user1121477-我会让start和stop按钮执行一个setInterval来开始调试,这个setInterval只执行一个console.log-然后检查chrome控制台是否正常工作。然后慢慢加入你所有的时钟。谢谢你的帮助。我已经将您提供的代码添加到页面正文的底部,但是按钮似乎仍然不起作用。我的页面中的哪些代码需要删除或更改?在Javascript方面并非最热门:P@user1121477-我会让start和stop按钮执行一个setInterval来开始调试,这个setInterval只执行一个console.log-然后检查chrome控制台是否正常工作。然后慢慢加入你所有的时钟材料。