Home >Web Front-end >JS Tutorial >JavaScript writes a timer

JavaScript writes a timer

高洛峰
高洛峰Original
2016-11-25 09:30:521199browse

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.









< ;button type="button" onclick="timer('ptime')">
Start timing





Statement:
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