Home >Web Front-end >JS Tutorial >JavaScript writes a timer
I am new to JavaScript and wrote a timer in JavaScript.
Design idea:
1. Use the Date() object to continuously obtain time points;
2. Then subtract the milliseconds of the two time points to calculate the time difference;
3. Accumulate the time difference, so that Can keep time accurately.
ps:
I haven’t seen this method on the Internet. Generally, setinterval is used. The delay and time control that come with the programming language have relatively large errors;
So use the system time subtraction method. The control is precise, and in this example, the three-digit millisecond number is displayed to verify the accuracy. The setting is 100 milliseconds, and Js itself will have errors;
This program can avoid inaccuracy problems.
This example is for mutual communication, and we are eager to get readers’ ideas and suggestions.