使用js的定时器setTimeout()实现加载进度条效果

效果图如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>New Web Project</title>
		<script type="text/javascript">
		
			var timer = null;
			var width = 0;
			function doStart() {
				timer = window.setTimeout("onChange()", 10);
			}

			function doStop() {
				window.clearTimeout(timer);
			}

			function onChange() {
				if(width == 400) {
					window.clearTimeout(timer);
				}else {
					width += 1;
					document.getElementById("div2").style.width = width + "px";
					
					timer = window.setTimeout("onChange()", 10);
				}
			}

		</script>
	</head>
	<body>
		<div style="height: 20px;width: 400px;border: 1px solid gray;padding: 2px">
			<div style="height:20px;width:0px;background-color: orange" id="div2">

			</div>
		</div>
		<br />
		<input type="button" value="开始" onclick="doStart()" />
		<input type="button" value="停止" onclick="doStop()" />
	</body>
</html>



你可能感兴趣的:(使用js的定时器setTimeout()实现加载进度条效果)