首页 新闻 赞助 找找看

类似团购倒计时怎么优化【仅剩5豆...】

0
悬赏园豆:5 [已关闭问题] 关闭于 2015-03-28 08:56

目前用到这么一个倒计时,但发现使用过程中,JS性能消耗严重(是不是遍历里局部变量过多?但这些变量好像只能在里面定义····),求解决方案:

html结构大概如下:


<div class="house-tuan-count">
<p>距离团购结束还有</p>
<div class="count-time" data-tiem="2015-05-12 12:33:12"><!-- 将团购结束时间填进来 -->
<span class="tuan-hours">00</span>:
<span class="tuan-minutes">00</span>:
<span class="tuan-seconds">00</span>
</div>
</div>

$(function(){
TimeDown('.count-time');
});

/** 日期比较 **/
function compareDate(strDate1,strDate2){
var date1 = new Date(strDate1.replace(/\-/g, "\/"));
var date2 = new Date(strDate2.replace(/\-/g, "\/"));
return date1-date2;
}

function TimeDown(_class) {
var nowDate = new Date();
$(_class).each(function(){
var endTime=$(this).attr('data-tiem');
endTime=Date.parse(endTime.replace(/-/g,"/"))*1;
var endDate = new Date(endTime);
//当前时间
//相差的总秒数
var totalSeconds = parseInt((endDate - nowDate) / 1000);
if(totalSeconds<0){
$(this).hide();
$(this).next('.end-tips').remove();
$(this).parents('.house-tuan-count').append('<p class="end-tips" style="color:#f00">活动已结束</p>');
}else{

//小时数
var hours = Math.floor(totalSeconds / (60 * 60));
var modulo = totalSeconds % (60 * 60);
//分钟
var minutes = Math.floor(modulo / 60);
//秒
var seconds = modulo % 60;
//console.log(endDate)
$(_class).show();
//输出到页面
$(this).find('.tuan-hours').text(hours);
$(this).find('.tuan-minutes').text(minutes);
$(this).find('.tuan-seconds').text(seconds);

//延迟一秒执行自己
setTimeout(function () {
TimeDown(_class);
}, 1000)

}
})
}

酸番茄的主页 酸番茄 | 初学一级 | 园豆:87
提问于:2015-03-28 00:42
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册