經(jīng)常逛購物網(wǎng)站的程序員都知道,每逢節(jié)假日購物網(wǎng)站上都會有物品搶購,這些商品在優(yōu)惠之前都會有倒計時,這個功能主要是前端來實現(xiàn),這需要后臺傳給前端一個時間,前端代碼如下
//搶購倒計時
function getOverTime(endTime){
var nowDate = new Date(); //開始時間,當前時間
var endDate = new Date(endTime); //結束時間,需傳入時間參數(shù)
var t = endDate.getTime()-nowDate.getTime(); //時間差的毫秒數(shù)
var d = 0,h = 0,m = 0,s = 0;
if(t>=0){
d = Math.floor(t/1000/3600/24);
h = Math.floor(t/1000/60/60%24);
m = Math.floor(t/1000/60%60);
s = Math.floor(t/1000%60);
}
rerurn "僅剩:"+d+"天"+h+"小時"+m+"分鐘"+s+"秒";
}