[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript"src="jquery/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(function(){
var time1;
//给“#start”绑定点击事件-设置定时器
$("#start").click(function(){
time1=setInterval(divtoggle(),1000);
});
/* //此种方法只能切换一次,并不能反复切换
function divtoggle(){
$("div").toggle(1000);
}
*/
//在toggle中再次设定时器能达到反复切换效果,但是定时器清不掉
function divtoggle(){
$("div").toggle(1000,function(){
// clearInterval(time1);
time1=setInterval(divtoggle(),1000)});
}
$("#stop").click(function(){
clearInterval(time1);
});
});
</script>
<style type="text/css">
div{
width:300px;
height:300px;
background:black;
}
</style>
</head>
<body>
<input type="button" value="开始"id="start"></input>
<input type="button" value="结束"id="stop"></input>
<div></div>
</body>
</html>