`

js前端传递时间段来倒计时

js 
阅读更多
上班整合了下这个 需要用到 做下记录

<script type="text/javascript">
/**
* 读秒
*/
function endTime(stime,etime,showId){
var time_now_server, time_now_client, time_end, time_server_client, timerID;
time_end = new Date(etime);//结束的时间
time_end = time_end.getTime();
time_now_server = new Date(stime);//开始的时间
time_now_server = time_now_server.getTime();
time_now_client = new Date();
time_now_client = time_now_client.getTime();
time_server_client = time_now_server - time_now_client;
setTimeout(function(){
show_time(time_end,time_server_client,showId);
}, 1000);
}

function show_time(time_end,time_server_client,showId) {
var timer = document.getElementById(showId);
if (!timer) {
return;
}
timer.innerHTML = time_server_client;
var time_now, time_distance, str_time;
var int_day, int_hour, int_minute, int_second;
var time_now = new Date();
time_now = time_now.getTime() + time_server_client;
time_distance = time_end - time_now;
if (time_distance > 0) {
int_day = Math.floor(time_distance / 86400000)
time_distance -= int_day * 86400000;
int_hour = Math.floor(time_distance / 3600000)
time_distance -= int_hour * 3600000;
int_minute = Math.floor(time_distance / 60000)
time_distance -= int_minute * 60000;
int_second = Math.floor(time_distance / 1000)
if (int_hour < 10)
int_hour = "0" + int_hour;
if (int_minute < 10)
int_minute = "0" + int_minute;
if (int_second < 10)
int_second = "0" + int_second;
str_time = "<span>"+int_day +"</span>"+ "天" +"<span>"+ int_hour + "</span>"+"小时" +"<span>" +int_minute + "</span>分钟"
+"<span>"+ int_second + "</span>秒";
timer.innerHTML = str_time;
setTimeout(function(){
show_time(time_end,time_server_client,showId);
}, 1000);
} else {
timer.innerHTML = timer.innerHTML;
clearTimeout(timerID)
}
}


</script>

<p class="center special" id="${goods.divId}">
                <#if goods.time_flag??>
                <#if goods.time_flag=='no'>
                活动结束
                <#else>
                敬请期待
                </#if>
                    <#else>
                <script>
                  endTime("${goods.start_time!''}","${goods.end_time!''}","${goods.divId}");
                    </script>
                </#if>
</p>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics