- window.setInterval("要执行的函数",毫秒数)
- window.clearInterval()
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
var demo = null;
function nowTime(){
// 获取当前时间
var showTime = Date().toLocaleString();
console.log(showTime)
// 让div上面显示的文本是获取到的当前时间
document.getElementById("timeDiv").innerHTML= showTime;
}
function start(){
// 每隔1S执行一次nowTime函数,用户动态显示时间
demo = window.setInterval("nowTime()",1000);
}
function stop(){
window.clearInterval(demo);
}
</script>
</head>
<body>
<div id="timeDiv"></div>
<input type="button" value="启动" onclick="start()" />
<input type="button" value="停止" onclick="stop()" />
</body>
</html>