<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<meta charset="utf-8" />
<script>
window.onload = function () {
var oBtn1 = document.getElementById('btn1');
var oBtn2 = document.getElementById('btn2');
var timer = null;
oBtn1.onclick = function () {
timer = setInterval(function () {
alert('a');
}, 5000);
};
oBtn2.onclick = function () {
clearInterval(timer);
};
};
/*
function show()
{
alert('a');
}
*/
//setInterval(show, 3000);
//setTimeout(show, 3000);
</script>
</head>
<body>
<input id="btn1" type="button" value="开启"/>
<input id="btn2" type="button" value="关闭" />
</body>
</html>
例子:
<pre name="code" class="javascript"><!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<meta charset="utf-8" />
<style>
#div1 {width:200px;height:200px;background:blue;
position:absolute;left:0;top:50px;
}
</style>
<script>
setInterval(function ()
{
var oDiv = document.getElementById('div1');
oDiv.style.left=oDiv.offsetLeft+10+'px';
}, 30);
</script>
</head>
<body>
<div id="div1">
</div>
</body>
</html>