JQuery 定时器

版权声明:转载请注明出处 https://blog.csdn.net/qq_40162735/article/details/82854934

首先我们模拟考试时间倒计时,来做一个简单的小例子。

显示倒计时DIV容器:

<div style=" padding:5px; font-size:15px; font-weight:bold; color:#E00; text-align:center; display: none;">
    倒计时:<span id="timer"> </span>
</div>

Js代码: 

        var maxtime = 10 * 60;//考试时间假设为10分钟
	function CountDown() {
		if (maxtime >= 0) {
				var minutes = Math.floor(maxtime / 60);//剩余分钟
				var seconds = Math.floor(maxtime % 60);//剩余秒

				if(minutes == 0){minutes="00";}
				if(seconds == 0){seconds="00";}
	
				if(minutes.toString().length == 1){minutes = "0"+minutes;};
				if(seconds.toString().length == 1){seconds = "0"+seconds;};

				msg = minutes + ":" + seconds + "";
				document.all["timer"].innerHTML = msg;
				--maxtime;
		} else {
			clearInterval(timer);
			alert("时间到,考试结束!");
		}
	}
	timer = setInterval("CountDown()", 1000);//每个一秒刷新页面倒计时间

猜你喜欢

转载自blog.csdn.net/qq_40162735/article/details/82854934