倒计时demo

661 阅读1分钟

一个超简单的倒计时小功能。 拿过来就能用(记得更换jq)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>倒计时demo</title>
</head>
<body>
	倒计时:<span id="time">00 : 00 : 00</span>
	<script src="https://quhongqiang.com/js/jquery.min.js"></script>
	<script>
	$(function(){
		countDown("900", "#time"); // 倒计时XX秒
	})
	function countDown(times, id){ 
		var hour, minute, second,
			watch = $(id),
		    timer = setInterval(function(){ 
			if (times > 0) { 
				times -= 1; 
				hour = Math.floor((times / 3600) % 24); 
				minute = Math.floor((times / 60) % 60); 
				second = Math.floor(times % 60); 
				hour = hour<10?"0"+hour:hour;            // 计算小时 
				minute = minute<10?"0"+minute:minute;    // 计算分钟 
				second = second<10?"0"+second:second;    // 计算秒杀 
				watch.text(hour).append(" : ").append(minute).append(" : ").append(second);
			} else { 
				clearInterval(timer);
			} 
		}, 1000); 
	} 
	</script>	
</body>
</html>