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>