JavaScript compiled a timer, a javascript Timer

Source: Internet
Author: User

JavaScript compiled a timer, a javascript Timer

A timer is written in JavaScript.

Design Philosophy:

1. Use the Date () object to continuously obtain time points;

2. Subtract the milliseconds between the two time points to calculate the time difference;

3. accumulate the time difference so that the timing can be accurate.


Ps:

If this method is not found on the internet, it generally uses setinterval, the latency inherent in the programming language, and the time control has a relatively large error;

Therefore, the system time subtraction method is used to control accuracy. In this example, the accuracy is verified by three milliseconds. The setting is 100 milliseconds, and Js itself has an error;

This program can avoid inaccurate problems.


In this example, we exchange ideas and suggestions with each other, and hope to get the readers.


<! DOCTYPE html> 



A timer implemented using javascript

<Html>
<Body>
<Script language = "javascript">
Var timeout =-1;
Var timer;
Function cal (){
If (startButton. value = ""){
StartButton. value = "paused ";
Timer = setInterval ("timeout ++; textField. value = 'already passed:' + (timeout/600) + 'minute. ';", 100)
}
Else {
ClearInterval (timer );
StartButton. value = "start ";
}
}
Function reset (){
StartButton. value = "start ";
TextField. value = "";
If (timer! = Null ){
Timeout =-1;

ClearInterval (timer );
}
}
</Script>
<Input type = "text" size = 50 name = "textField">
<Input type = "submit" size = 10 name = "startButton" value = "start" onclick = "cal ();">
<Input type = "submit" size = 10 name = "resetButton" value = "reset" onclick = "reset ();">
</Body>
</Html>

Use javascript to write a running table Timer

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "www.w3.org/..al.dtd">
<Html xmlns = "www.w3.org/5o/xhtml">
<Head>
<Meta content = "text/html; charset = gb2312" http-equiv = "content-type"/>
<Title> Untitled Page </title>
</Head>
<Body>
<Center>
<Input id = "showtime" type = "text" disabled = "disabled" value = "00: 00: 00: 000 "/> <br/> <input id =" pause "type =" button "value =" start "/> <input id =" reset "type =" button "value = "reset"/>
</Center>
</Body>
</Html>
<Script type = "text/javascript">
Var tag = false;
Var t;
Var thisdate = new Date ('0', '0', '0', '0', '0', '0 ');
Document. getElementById ("reset"). onclick = function (){
ClearTimeout (t );
Tag = false;
Document. getElementById ("pause"). value = "start ";
Document. getElementById ("showtime"). value = "00: 00: 00: 000 ";
... The remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.