Use JS to create a stopwatch accurate to 10ms (code attached)

不言
Release: 2018-08-07 11:44:45
Original
2051 people have browsed it

The content of this article is about using JS to realize the production of a stopwatch that can be accurate to 10ms (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

I have just come into contact with js during this period and want to use the knowledge I have learned to make a simple stopwatch.

The ideas for making a stopwatch are as follows:

1.First determine the function and interface.

My purpose is to make the simplest stopwatch, so I only need the start, end and clear functions. The interface is as shown below:

Not started running:

Running:

2. Conceive the implementation method.

First of all, the core method is definitely the setInterval() method, which is used to display the time periodically. Because I want to be accurate to 10ms, I set the time interval to 10.

Furthermore, how to increase the time?

A . What I started to think of was setting variables milliSeconds, seconds, minutes, and hours. milliSeconds increments by 1 every 10ms, when When milliSeconds >= 100, use milliSeconds modulo 100, and at the same time, seconds is increased by 1. In the same way, when seconds reaches 60 minutes is incremented by 1, and hours is incremented by 1 when minutes reaches 60.

However, in order to ensure the uniformity of the format (I want to display 02:01:24:06 instead of 2:1:24:6), I changed the four variables into 8 variables. , the same thoughts as above. (For the code, see “Stopwatch with Delay” at the end of this page).

However, there is a delay problem during operation, and the delay will accumulate. It can still run relatively accurately when the time is short. As time goes by, the time on the stopwatch will deviate greatly from the standard time.

B . In order to reduce the delay, I designed another method (actually this method has a higher delay than the previous one). At this time, only one time variable is used to record the trigger start The number of milliseconds that have passed since the button was pressed (time is in 10ms, a/b/c/d below represents milliseconds (10ms), seconds, minutes, and hours). In order to pursue the unity of format, I added if statement, when a/b/c/d is less than 10, add 0 placeholder in front.

var a=time%100; var b=time%6000/100; var c=time%360000/6000; var d=time%8640000/36000;
Copy after login

Compare plan A with plan B. Since A uses a nested form to calculate time, when the time is short, plan A requires fewer judgments and is more efficient; while for plan B, each cycle must go through four calculations. Therefore, in When the time is short, the efficiency is even lower than A.

#C . In order to synchronize with the real time without any error, I thought of another way. In the JS Date object, there is a method called getTime(), which is used to return the number of milliseconds since January 1, 1970. When start is clicked, getTime() is triggered, and this time is used as the basis. getTime() is executed every ten milliseconds, and the latter is subtracted from the former to obtain the relative time. In this way, the problem of synchronization with real time is perfectly solved.

Attached are three pieces of code:

Code 1

   有延迟的秒表  
  
00 : 00 : 00 : 00
Copy after login

Code 2

   仍然有延迟的秒表  
  
00 : 00 : 00 : 00
Copy after login

Code 3 (code synchronized with time)

   秒表  
  
00 : 00 : 00 : 00
Copy after login

Recommended related articles:


The above is the detailed content of Use JS to create a stopwatch accurate to 10ms (code attached). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!