ajax 拼接数据 后台时间戳倒计时

 1.拼接获取到时间戳,给span 绑定data-time="'+classes.activity_end+'"

2.在拼接之后放入父元素内的时候 获取到所有的时间戳  循环,  调用countDown方法来实现页面

function countDown(dateline,elem){
var timer=null,
    times=dateline - new Date()/1000;
if(times <= 0){
    return 0 + '天' + 0 + '时' + 0 + ' 分' + 0 + '秒';
}
 
timer=setInterval(function(){
    var current_time=Math.floor(new Date()/1000)
times=dateline-current_time;
if(times<=0){
    clearInterval(timer);
$(elem).html( 0 + '天' + 0 + '时' + 0 + ' 分' + 0 + '秒');
    }
var day=0,
hour=0,
minute=0,
second=0;//时间默认值
if(times > 0){
day = Math.floor(times / (60 * 60 * 24));
hour = Math.floor((times- day * 24*60 * 60) / (60 * 60)) ;
minute = Math.floor((times- (day * 24 * 60* 60) - (hour * 60* 60) )/ 60) ;
second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
}
if (day <= 9) day = '0' + day;
if (hour <= 9) hour = '0' + hour;
if (minute <= 9) minute = '0' + minute;
if (second <= 9) second = '0' + second;
$(elem).html(day + '天' + hour + '时' + minute + ' 分' + second + '秒');

},1000);
}
posted @ 2018-12-20 14:37  吃饭睡觉,打豆豆  阅读(372)  评论(0编辑  收藏  举报