// 参数分别为 开始时间即某个特定时间 时长即多长时间 页面id名将计算好的倒计时放在页面上显
示
time(startTime, timelong, idname) {
let curTime = new Date(startTime); // curTime: Tue Oct 13 2020 16:44:47 GMT+0800
(中国标准时间)
// 2. 在当前时间curTime变量上加上10分钟
let startAndtimes = new Date(
curTime.setMinutes(curTime.getMinutes() + timelong)
); //开始时间 + 广播时长 Tue Oct 13 2020 17:54:47 GMT+0800 (中国标准时间)
let nowTime = new Date();
// 开始时间加上广播时长 大于 现在时间 开始进行 倒计时
if (startAndtimes.getTime() > nowTime.getTime()) {
this.timeInterval(startTime, timelong, idname);
// console.log(idname, "jjnjjnjnjnj");
}
},
//倒计时方法
daojishiTime(startTime, timelong, idname) {
let curTime = new Date(startTime); // curTime: Tue Oct 13 2020 16:44:47 GMT+0800
(中国标准时间)
// 2. 在当前时间curTime变量上加上10分钟
let startAndtimes = new Date(
curTime.setMinutes(curTime.getMinutes() + timelong)
); //开始时间 + 广播时长 Tue Oct 13 2020 17:54:47 GMT+0800 (中国标准时间)
let now = new Date();
// 获取时间差,单位“毫秒”
let disTime = Math.abs(startAndtimes.getTime() - now.getTime());
// 判断是否时间到了指定点
// 是,就终止计时器,终结函数运行。
// if( disTime===0 ){
// showTime.innerHTML = "时间到~!";
// clearInterval(myset);
// return false;
// }
disTime = Math.floor(disTime / 1000); // 把毫秒,转为秒
let miao = disTime % 60; // 秒
let fen = Math.floor((disTime / 60) % 60); // 分
let xiaoshi = Math.floor(disTime / 60 / 60) % 24; // 小时
let tian = Math.floor(disTime / 60 / 60 / 24); // 天
// 显示时间。利用了 ES6 的模版字符串,好处是可以存放变量。
let showTime = document.getElementById(idname);
showTime.innerHTML = `${tian}天${xiaoshi}时${fen}分${miao}秒`;
// return `${tian}天${xiaoshi}时${fen}分${miao}秒`;
// console.log(
// `${tian}天${xiaoshi}时${fen}分${miao}秒`,
// "-==========================----"
// );
},
// 启动计时器,让时间走动起来。
timeInterval(startTime, timelong, idname) {
setInterval(() => {
this.daojishiTime(startTime, timelong, idname);
}, 1000);
},
实现特定时间与现在时间的差值进行倒计时
最新推荐文章于 2022-12-30 18:23:22 发布