js 秒的倒计时,将秒转换为时分秒显示

在VUE 中的使用

{{moveMin}}
// ...
methods: {
   // 补0 formatBit (val) { val
= +val return val > 9 ? val : '0' + val },
   // 秒转时分秒,求模很重要,数字的下舍入 formatSeconds (time) {
let min = Math.floor(time % 3600) let val = this.formatBit(Math.floor(time / 3600)) + ':' + this.formatBit(Math.floor(min / 60)) + ':' + this.formatBit(time % 60) return val },
   // 定时器 minReturn () {
let time = 5 let t = setInterval(() => { time-- this.moveMin = this.formatSeconds(time) if (time <= 0) { clearInterval(t) } }, 1000) } }

 附:向上向下取整等

parseInt(5/2) //丢弃小数部分,保留整数部分 结果为2
Math.floor(5.55) //向下取整 结果为5 
Math.floor(5.99) //向下取整 结果为5 
Math.ceil(5.21) //向上取整,结果为6 
Math.ceil(5.88) //向上取整,结果为6 
Math.round(5.78) //四舍五入 结果为6 
Math.round(5.33) //结果为5 

.

原文地址:https://www.cnblogs.com/xiangsj/p/11483677.html