首页 > 代码库 > 每天一个JavaScript实例-使用带有定时器的函数闭包

每天一个JavaScript实例-使用带有定时器的函数闭包

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>每天一个JavaScript实例-使用带有定时器的函数闭包</title>
<style>
	#redbox{
			position:absolute;
			left:100px;
			top:100px;
			width:200px;
			height:200px;
			background-color:red;
		}
</style>
</head>

<body>
<div id ="redbox"></div>

<script>
window.onload = function(){
	document.getElementById("redbox").onclick = move;
	}
	function move(){
			var x = 100;
			intervalId = setInterval(function(){x+=5;var left = x +"px";document.getElementById("redbox").style.left = left;},100)
		}
</script>
</body>
</html>

每天一个JavaScript实例-使用带有定时器的函数闭包