在页面上实现倒计时的功能,如下图:
代码:
js:
//获取服务器当前时间
var serverTime;
$(document).ready(function(){
serverTime = new Date($.ajax({async: false}).getResponseHeader("Date")).getTime();
});
//
var asd = setInterval(showTime, 1000);
function showTime(){
$(".asd").each(function(){
var createTime = $(this).attr("data");//获取开始时间
var nowTime = serverTime;//用服务器的当前时间
var time = createTime - (nowTime - 864000000);//此处864000000为倒计时时长的毫秒数
var day = parseInt(time /1000 / 60 / 60 / 24);
var hour = parseInt(time /1000 / 60 / 60 % 24);
var minute = parseInt(time / 1000 / 60 % 60);
var seconds = parseInt(time / 1000 % 60);
$(this).html("还剩" + day + "天" + hour + "时" + minute + "分" + seconds + "秒");
});
serverTime = serverTime - (- 1000);
}
html:
<span class="asd" data="${order.createTime.getTime()}"></span>
另:时间统一问题可以参考https://blog.csdn.net/baidu_23275675/article/details/82898519