简体   繁体   English

使用 Javascript 创建秒表:点击事件有问题

[英]Creating stopwatch using Javascript: Having trouble with click event

I'm currently working on this Mozilla Developer Network's project .我目前正在研究这个 Mozilla 开发者网络的项目 The assignment is to create a stop watch using the date object and some plain old Javascript, and things are coming together slowly.任务是使用日期 object 和一些普通的旧 Javascript 创建一个秒表,事情进展缓慢。

I've figured out the logic behind showing the elapse of time using the Date.now() method, and have been able to show a successful elapse of time.我已经弄清楚了使用 Date.now() 方法显示时间流逝背后的逻辑,并且能够显示成功的时间流逝。 Only issue is, this only happens when the html element is clicked.唯一的问题是,这仅在单击 html 元素时发生。 I want the elapse of time to continue showing on the screen without having to keep clicking.我希望时间流逝可以继续显示在屏幕上,而无需继续单击。

Does anyone know how I could do this?有谁知道我该怎么做?

Here's the link to the assignment: https://github.uconn.edu/pages/ssw19002/dmd-3475/Week-9/stop-watch/stop-watch.html这是作业的链接: https://github.uconn.edu/pages/ssw19002/dmd-3475/Week-9/stop-watch/stop-watch.html

Here's the code so far:这是到目前为止的代码:

function displayTime() {
  let date = new Date();
  let time = date.toLocaleTimeString();
  document.querySelector('.clock').textContent = time;
}


const createClock = setInterval(displayTime,1000);


//add click event to start button
let startBtn = document.getElementById("start-button");

startBtn.addEventListener('click', function(){
  document.querySelector('.clock').textContent = setInterval(function startTime(){startTime - Date.now()}, 1000);

  const createClock = setInterval(startTime,1000);
});

Here is one I made for command line use.这是我为命令行使用而制作的。 Perhaps you can modify it for browser use:也许您可以修改它以供浏览器使用:

function format(d) {
   let mil = String(d % 1000).padStart(3, '0');
   d = Math.trunc(d / 1000);
   let sec = String(d % 60).padStart(2, '0');
   d = Math.trunc(d / 60);
   return String(d) + ' m ' + sec + ' s ' + mil + ' ms';
}

function main() {
   console.clear();
   let s = format(Date.now() - n);
   console.log(s);
}

let n = Date.now();
setInterval(main, 10);

https://89z.github.io/autumn/return-duration-string/js https://89z.github.io/autumn/return-duration-string/js

Just the first two definitions should be enough for the clock part.对于时钟部分来说,前两个定义就足够了。 setInterval will start an interval that calls displayTime every second. setInterval将启动一个间隔,每秒调用一次displayTime Make a second function displayElapsedTime that is similar to displayTime , and inside the startBtn click handler, stop the displayTime interval (you'll need to hold onto the result of the setInterval call to be able to do this) and start the displayElapsedTime interval.制作第二个 function displayElapsedTime ,它类似于displayTime ,在startBtn单击处理程序中,停止displayTime间隔(您需要保持setInterval调用的结果才能执行此操作)并启动displayElapsedTime间隔。 As always, read the best / most official documentation you can find on how to do this. 与往常一样,请阅读您能找到的有关如何执行此操作的最佳/最官方文档。

The code you posted has a bug - the body of the function you named startTime contains a reference to a variable named startTime - which will then point to the function startTime .您发布的代码有一个错误 - 您命名为startTime的 function 的主体包含对名为startTime的变量的引用 - 然后它将指向 function startTime It seems you named the function itself startTime instead of naming a variable defined inside the function.您似乎将function本身命名为 startTime,而不是命名在 function 中定义的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM