您的位置:首页 > 其它

时间倒计时

2015-11-03 17:42 218 查看
var startTime = $("#sendTime #time").html().split("-");
var stopTime = $("#stopTime #time").html().split("-");
var start0 = startTime[0], start1 = startTime[1], start2 = startTime[2];
var stop0 = stopTime[0], stop1 = stopTime[1], stop2 = parseInt(stopTime[2]) + 1;
var day1 = new Date(start0, start1 - 1, start2).getTime();
var day2 = new Date(stop0, stop1 - 1, stop2).getTime();
var today = new Date();
var ms, dt;
ms = day2 - today;
dt = Math.ceil(ms / 1000 / 3600 / 24);
if (ms > 0) {
$("#last").html("(剩余约" + dt + "天)");
} else if (ms == 0) {
$("#last").html("(剩余约" + 1 + "天)");
} else {
$("#last").html("(已过期)");
}


View Code
一个封装好的时间倒计时,两个时间戳可以做

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>倒计时</title>
</head>
<body>
<script>
function countDown( maxtime,fn )
{
var timer = setInterval(function()
{
if(maxtime>=0){
d=parseInt(maxtime/3600/24);
h=parseInt((maxtime/3600)%24);
minutes=parseInt((maxtime/60)%60);
seconds=parseInt(maxtime%60);

//minutes = Math.floor(maxtime/60);
//seconds = Math.floor(maxtime%60);
msg = "距离结束还有"+d+"天"+h+"小时"+minutes+"分"+seconds+"秒";
fn(msg);
if(maxtime == 5*60)
alert('注意,还有5分钟!');
--maxtime;
}
else{
clearInterval( timer );
fn("时间到,结束!");
}
}, 1000);
}
</script>

<div id="timer1" style="color:red"></div>

<script>
countDown( 6000,function( msg ) //6000服务器时间差 单位为妙
{
document.getElementById('timer1').innerHTML = msg;
});
</script>

<div id="timer2" style="color:red"></div>
<script>
countDown( 6000,function( msg )
{
document.getElementById('timer2').innerHTML = msg;
});
</script>
<div id="timer3" style="color:red"></div>

<script>

countDown( 600000,function( msg )
{
document.getElementById('timer2').innerHTML = msg;
});

countDown(20,function( msg )
{
document.getElementById('timer3').innerHTML = msg;
})
</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: