setTimeout, setInterval

代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<h4>现在的时间为:<span id="a"></span></h4>

<script type="text/javascript">
showTime();
function showTime(){
var d = new Date();
var a = document.getElementById("a");
a.innerHTML
= "This time " + d.toLocaleString();
setTimeout(
"showTime()", 1000);
}
</script>

<h4>这里的内容是<span id="content"></span></h4>
<input type="button" value="start" onclick="timer = setInterval('startShow()', 1000)" />
<input type="button" value="stop" onclick="stopShow()" />
<script type="text/javascript">
var iValue = 1;
//var timer = null;
var cont = document.getElementById("content");
//var timer = setInterval("startShow()", 1000);
function startShow(){
cont.innerHTML
= cont.innerHTML + (iValue++) + " - ";
//timer = setTimeout("startShow()", 1000);
}
function stopShow(){
clearInterval(timer);
}
</script>

</body>
</html>
通过这个小练习,掌握了setTimeout,setInterval的用法。

原文地址:https://www.cnblogs.com/jikey/p/1680617.html