js中定时器之一

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
var id;
function startInterval(){
id = setInterval(function(){
alert("interval evaling");
},3000);
}

function testClear(){
clearInterval(id);
}

function testCount(){
var count = 0;
var date;
setInterval(function(){
count++;
//console.log(count);
date = new Date();



//document.getElementById("count").innerHTML = ""+count;
document.getElementById("count").innerHTML = date.getFullYear()+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日 "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds()+":"+date.getMilliseconds();
},1)
}

/**
*
*/

</script>
</head>
<body>
<input type="button" onclick="startInterval()" value="interval"/>
<input type="button" onclick="testClear()" value="clear"/>
<input type="button" onclick="testCount()" value="count">
<div id="count" style="color: red;">
0
</div>
</body>
</html>
原文地址:https://www.cnblogs.com/hwgok/p/5730311.html