Several realization methods of javascript/jquery--simple timer

Source: Internet
Author: User
This article for you to introduce the use of javascript/jquery to achieve a simple timer, there are a variety of methods, we can choose according to their own preferences, I hope to help you

The first method:

Copy Code code as follows:


<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%>


<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


<html>


<head>


<title> Timer </title>


<script language= "javascript" src= "Jquery-1.4.2.js" ></script>


<script language= "JavaScript" >


//Use setinterval intermittent calls (this method is not recommended)


$ (function () {


setinterval (function () {


$ ("#currentTime"). Text (New Date (). toLocaleString ());


},1000);


});


</script>


</head>


<body>


<div id= "CurrentTime" ></div>


</body>


</html>


The second method:

Copy Code code as follows:


<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "


pageencoding= "UTF-8"%>


<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


<head>


<title> Timer </title>


<script language= "javascript" src= "Jquery-1.4.2.js" ></script>


<script language= "JavaScript" >


//Use settimeout timeout call


function StartTime () {


$ ("#currentTime"). Text (New Date (). toLocaleString ());


settimeout (' StartTime () ', 1000);


}


</script>


</head>


<body onload= "StartTime ()" >


<div id= "CurrentTime" ></div>


</body>


</html>


The third method:

Copy Code code as follows:


<%@ page language= "java" contenttype= "text/html charset=utf-8" pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title> timer </title>
<script type=" Text/javascript ">
var c=0;
var t;
Function Timedcount () {
document.getElementById (' txt '). value=c;
C=c+1;
T=settimeout ("Timedcount ()", 1000);
}
Function Stopcount () {
cleartimeout (t);
}
</script>

<body>
<form>
<input type= button "value=" Start meter When "Onclick=" Timedcount () "/>
<input type=" text "id=" txt "/>
<input type=" button "value=" Stop timing! "Onclick=" Stopcount () "/>
</form>
</body>

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.