js 自定义倍速时钟

调用

let clock = new Clock()
let speed = 1
clock
.init({ time: new Date('2021-4-4 2:2:59'), speed: 1 })
.start()
.setSpeed(speed)
.stop()
setInterval(() => {
console.log(clock.getChineseTime());
// console.log(clock.getTime());
}, 1000 / speed)

clock

class Clock {
constructor(params) {
this.time
this.speed
this.year
this.month
this.day
this.hour
this.minute
this.second
this.s // 存放时间字符串
this.flag // 是否连续更新时间
return this
}
init(params) {
try {
this.time = params.time
this.speed = params.speed
this.year = this.time.getFullYear()
this.month = this.time.getMonth() + 1
this.day = this.time.getDate()
this.hour = this.time.getHours()
this.minute = this.time.getMinutes()
this.second = this.time.getSeconds()
this.s = `${this.year} ${this.month} ${this.day} ${this.hour} ${this.minute} ${this.second}`.split(' ')
return this
} catch (error) {
console.warn("参数错误", error);
}
}
stop() {
this.flag = 0
return this
}
start() {
this.flag = 1
this.increase()
return this
}
setSpeed(params) {
this.speed = params
return this
}
getChineseTime() {
return this.formatTimeToChinese(this.s.join(' '))
}
getTime() {
return `${this.s[0]}-${this.s[1]}-${this.s[2]} ${this.s[3]}:${this.s[4]}:${this.s[5]}`
}
sum(a, b) {
try {
return Number(a) + Number(b)
} catch (error) {
console.warn(error);
}
}
// // 参数time为空格间隔的字符串时间
formatTimeToChinese(time) {
time = time.split('')
'年月日时分秒'.split('').forEach(e => {
for (let i = 0; i < time.length; i++) {
let t = time[i]
if (t === ' ') {
time[i] = e
return
}
}
time.push(e)
})
return time.join('')
}
increase() {
let _ = this
setTimeout(() => {
// 进行时间计算,进来先加1s
let remainder
let multiple
_.s[5] = _.sum(_.s[5], 1)
for (let index = _.s.length - 1; index >= 0; index--) {
switch (index) {
// 秒
case 5:
{
let t = _.s[index]
multiple = parseInt(t / 60)
remainder = t % 60
if (multiple >= 1) {
_.s[index - 1] = _.sum(_.s[index - 1], multiple)
}
_.s[index] = `${remainder}`.padStart(2, '0')
break;
}
// 分
case 4:
{
let t = _.s[index]
multiple = parseInt(t / 60)
remainder = t % 60
if (multiple >= 1) {
_.s[index - 1] = _.sum(_.s[index - 1], multiple)
}
_.s[index] = `${remainder}`.padStart(2, '0')
break;
}
// 时
case 3:
{
let t = _.s[index]
multiple = parseInt(t / 24)
remainder = t % 24
if (multiple >= 1) {
_.s[index - 1] = _.sum(_.s[index - 1], multiple)
}
_.s[index] = `${remainder}`.padStart(2, '0')
break;
}
// 日
case 2:
{
let t = _.s[index]
let fullMoon = '1 3 5 7 8 10 12'
let moon = '4 6 9 11'
if (fullMoon.indexOf(_.month) + 1 > 0) {
multiple = parseInt(t / 31)
remainder = t % 31
if (multiple > 1) {
_.s[index - 1] = _.sum(_.s[index - 1], multiple)
}
} else if (moon.indexOf(_.month) + 1 > 0) {
multiple = parseInt(t / 30)
remainder = t % 30
if (multiple > 1) {
_.s[index - 1] = _.sum(_.s[index - 1], multiple)
}
} else if (Number(_.month) === 2) {
multiple = parseInt(t / 28)
remainder = t % 28
if (multiple > 1) {
_.s[index - 1] = _.sum(_.s[index - 1], remainder)
}
}
_.s[index] = `${_.s[index]}`.padStart(2, '0')
break;
}
// 月
case 1:
{
let t = _.s[index]
multiple = parseInt(t / 12)
remainder = t % 12
if (multiple > 1) {
_.s[index - 1] = _.sum(_.s[index - 1], remainder)
}
_.s[index] = `${_.s[index]}`.padStart(2, '0')
break;
}
// 年
case 0:
{
break;
}
default:
break;
}
}
// console.log(_.formatTimeToChinese(_.s.reverse().join(' ')));
if (_.flag)
_.increase()
}, 1000 / _.speed)
}
}
posted @   lambertlt  阅读(237)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示