随机点名器【定时器练习】

要求:

开始和停止在一个按钮上

按钮可以重复使用-开始-停止-开始-停止--

做法:

主要是给按钮添加点击事件,使用计数器%2判断当前点击是开始还是停止并相应给出样式内容变换

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<style>
		#div1{
			width: 150px;
			height: 100px;
			background-color: orangered;
			padding-top: 25px;		
		}
		#demo{
			width: 100px;
			height: 50px;
			background-color: lightgrey;
			line-height: 50px;/*行高=高度  垂直居中*/
			margin-left: 25px;
			font-family: "微软雅黑";
			font-weight: bold;
			text-align: center;
		}
		#btn{
			margin-left:50px;
		}
	</style>
	<body>
		<div id="div1">
			<div id="demo">随机点名器</div>
			<button type="button" id="btn">开始</button>
		</div>
	</body>
	<script>
			var count=0;
			var flag1;
			function changeName(){
				var arr=new Array("唐三","小舞","戴沐白","朱竹清","香肠大叔","胖子","赵无极");
				var randomNum=Math.floor(Math.random()*arr.length);
				document.getElementById("demo").innerHTML=arr[randomNum];
			}
			
			document.getElementById("btn").onclick=function(){
				console.log(count);
				count++;
				if(count%2!=0){
					flag1=window.setInterval("changeName()",100);
					this.style.backgroundColor="red";
					this.innerHTML="开始";
					
				}else{
					this.innerHTML="停止";
					this.style.backgroundColor="green";
					window.clearInterval(flag1);
				}	
			}
		</script>
</html>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值