其他分享
首页 > 其他分享> > js倒计时 天,时,分,秒

js倒计时 天,时,分,秒

作者:互联网

在这里插入图片描述

<!doctype html>  
<html>  

    <head>  
        <meta charset="UTF-8">  
        <title>js倒计时</title>  
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />  
    </head>  

    <body> 
		
		<body>
			<div id="time"></div>
		</body>
		
		<script type="text/javascript" charset="utf-8">
				
				function count_down(time){
					//延迟一秒执行自己
					var iCount = setInterval(function () {
						//结束时间
						var endDate = new Date(time);
						//当前时间
						var nowDate = new Date();
						//相差的总秒数
						var totalSeconds = parseInt((endDate - nowDate) / 1000);
						
						//关闭定时器
						if(totalSeconds<=0) {
							clearInterval(iCount);
							alert("执行完毕");
							return;
						}
						
						//天数
						var days = Math.floor(totalSeconds / (60 * 60 * 24));
						//取模(余数)
						var modulo = totalSeconds % (60 * 60 * 24);
						//小时数
						var hours = Math.floor(modulo / (60 * 60));
						modulo = modulo % (60 * 60);
						//分钟
						var minutes = Math.floor(modulo / 60);
						//秒
						var seconds = modulo % 60;
						
						var t = days<10?'0'+days.toString():days;
						var s = hours<10?'0'+hours.toString():hours;
						var f = minutes<10?'0'+minutes.toString():minutes;
						var m = seconds<10?'0'+seconds.toString():seconds;
						
						var remaining_time = "还剩:" + t + "天" + s + "小时" + f + "分钟" + m + "秒";
						document.getElementById("time").innerHTML = remaining_time;
					}, 1000)
				}
				
				count_down("2019-07-01 12:00:00");
		</script>
    </body>  

</html>  

标签:function,js,倒计时,Date,nowDate,var,new
来源: https://blog.csdn.net/qq_39816586/article/details/94384447