Beginner JavaScript, write a timer in JavaScript.
Design ideas:
1, with the help of date () object, to continue to obtain time points;
2, then use two times the millisecond number of time points subtraction, calculate the time difference;
3, the cumulative time difference, so that the timing can be accurate.
Ps:
do not see on the internet there is such a method, is generally used setinterval, programming language comes with the delay, the time control has the bigger error;
so the system time subtraction method to control precision, and This example shows the three-bit millisecond digital confirmation accuracy, set is 100 milliseconds, and JS itself will have errors;
This program can avoid imprecise problems.
This example communicates with each other and is eager to get the ideas and suggestions of the readers.
<! DOCTYPE html>
JavaScript has written a timer