封装倒计时方法

/**
* @description: 倒计时开始时间点targetTime 参数
* @return 格式时间
*/

export const cutDownTime = (targetTime) => {
  const currentSecond = parseInt(new Date().getTime() / 1000 + '');
  let result = moment(targetTime).unix() - currentSecond;
  let cutTime = '';
  let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600) > 99 ? '99' : Math.floor(result / 3600);
  let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  let s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
  if (result <= 0) {
    cutTime = '00:00';
  } else {
    cutTime = h + ':' + m + ':' + s;
  }
  return cutTime;
}

 

posted @ 2021-10-20 10:39  天官赐福·  阅读(43)  评论(0编辑  收藏  举报
返回顶端