关于延时函数的简单应用--随机颜色不同速度切换

<!DOCTYPE html>
<html>
<head>
	<title>改变颜色</title>
	<meta charset="utf-8">
	<style type="text/css">
		#bordercolor{
			width: 400px;
			height: 400px;
			margin: 0 auto;
		}
	</style>
</head>
<body>
	<div id="bordercolor"></div>
	<div style="margin: 20px auto;width: 150px">
		<input type="button" name="" value="+" onclick="add(1);">
		<input type="button" name="" value="开始" onclick="begin();">
		<input type="button" name="" value="-" onclick="add(0);">
	</div>
	<script type="text/javascript">
/*
		//随机颜色
		function randomHexColor() { //随机生成十六进制颜色
			 var hex = Math.floor(Math.random() * 16777216).toString(16); //生成ffffff以内16进制数
			 while (hex.length < 6) { //while循环判断hex位数,少于6位前面加0凑够6位
			  hex = '0' + hex;
			 }
			 return '#' + hex; //返回‘#'开头16进制颜色
		}
*/
		var tar;//定义一个全局的变量
		var speed = 1000;
		//改变颜色的函数
		function changeColor(){
			document.getElementById("bordercolor").style.backgroundColor = randomColor();
		}
		//随机颜色的函数
		function randomColor(){
			var arr = [0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F"];
			var restcolor = "#";
			for(var i = 0; i < 6; i++){
				restcolor = restcolor + arr[Math.floor(Math.random() * 16)];
			}
			return restcolor;
		}
		//开始的按键
		function begin(){
			tar = setInterval(changeColor,speed);
		}
		//加减的按键
		function add(player){
			if (player == 1) {
				clearInterval(tar);
				speed = speed + 1000;
				tar = setInterval(changeColor,speed);
			}else if (player == 0) {
				clearInterval(tar);
				speed = speed - 1000;
				tar = setInterval(changeColor,speed);
			}
		}
	</script>
</body>
</html>
随机颜色的函数,一个借的网上的,一个自己用数组写的。自己写的不好看,网上的更简洁。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值