首页 > 其他分享 >js 自定义倍速时钟

js 自定义倍速时钟

时间:2022-10-10 08:44:32浏览次数:50  
标签:index multiple 自定义 sum js let time 倍速 remainder

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 参数time为空格间隔的字符串时间
        function 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('')
        }
        function diyTime(params) {
            try {
                let time = params.time
                let speed = params.speed
                let year = time.getFullYear()
                let month = time.getMonth() + 1
                let day = time.getMinutes()
                let hour = time.getHours()
                let minute = time.getMinutes()
                let second = time.getSeconds()
                // 间隔
                let interval = parseInt(1 / (1 / speed))
                let s = `${year} ${month} ${day} ${hour} ${minute} ${second}`.split(' ')
                let t
                console.log(s.join(' '));
                function sum(a, b) {
                    try {
                        return Number(a) + Number(b)
                    } catch (error) {
                        console.warn(error);
                    }
                }
                function increase() {
                    t = setTimeout(() => {
                        // 进行时间计算
                        s[5] = sum(s[5], interval)
                        let remainder
                        let multiple
                        s.reverse().forEach((e, index) => {
                            switch (index) {
                                // 秒
                                case 0:
                                    {
                                        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
                                        }
                                        break;
                                    }
                                // 分
                                case 1:
                                    {
                                        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
                                        }
                                        break;
                                    }
                                // 时
                                case 2:
                                    {
                                        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
                                        }
                                        break;
                                    }
                                // 日
                                case 3:
                                    {
                                        let t = s[index]
                                        let fullMoon = '1 3 5 7 8 10 12'
                                        let moon = '4 6 9 11'
                                        if (fullMoon.indexOf(year) + 1 > 0) {
                                            multiple = parseInt(t / 31)
                                            remainder = t % 31
                                            if (multiple > 1) {
                                                s[index + 1] = sum(s[index + 1], multiple)
                                                s[index] = remainder
                                            }
                                        } else if (moon.indexOf(year) + 1 > 0) {
                                            multiple = parseInt(t / 30)
                                            remainder = t % 30
                                            if (multiple > 1) {
                                                s[index + 1] = sum(s[index + 1], multiple)
                                                s[index] = remainder
                                            }
                                        } else if (Number(year) === 2) {
                                            multiple = parseInt(t / 28)
                                            remainder = t % 28
                                            if (multiple > 1) {
                                                s[index + 1] = sum(s[index + 1], remainder)
                                                s[index] = remainder
                                            }
                                        }
                                        break;
                                    }
                                // 月
                                case 4:
                                    {
                                        let t = s[index]
                                        multiple = parseInt(t / 12)
                                        remainder = t % 12
                                        if (multiple > 1) {
                                            s[index + 1] = sum(s[index + 1], remainder)
                                            s[index] = remainder
                                        }
                                        break;
                                    }
                                // 年
                                case 5:
                                    {
                                        break;
                                    }

                                default:
                                    break;
                            }
                        })
                        console.log(formatTimeToChinese(s.reverse().join(' ')));
                        clearTimeout(t)
                        increase()
                    }, 1000)
                }
                increase()
            } catch (error) {
                console.warn("参数错误", error);
            }
        }
        diyTime({ speed: 1000, time: new Date() })
    </script>
</body>

</html>

标签:index,multiple,自定义,sum,js,let,time,倍速,remainder
From: https://www.cnblogs.com/lambertlt/p/16774368.html

相关文章

  • JS基本类型和引用类型的存储方式
    一、基本类型:number,string,boolean,null,undefined,放在栈,栈内存是自动分配内存的。①基本类型比较是值的比较。②基本类型是按值访问,可以直接得到变量值。储存类型:值类......
  • react使用antd,按需引入,自定义主体
    -antd官网:https://ant.design/components/安装:npminstallantd简单使用:importReactfrom'react'import'./App.css'import{Button,DatePicker}from'ant......
  • definitions.json
     AdvancedRestClient的下载包链接:​​下载包​​​ 提取码:ofl3  还需要一个definitions.json 文件,从网上找的时候,都是需要积分的。然后去github上找了源码definit......
  • C# 遍历所有目录并在内部查找所有“sales.json”文件
    usingSystem;usingSystem.IO;usingSystem.Collections.Generic;namespacefiles_module{classProgram{staticvoidMain(string[]args){......
  • Springboot异常处理和自定义错误页面及@ControllerAdvice 注解的三种使用场景!五、@Con
    一、前言springboot默认发生4xx错误时候,pc端响应的页面如下如果是移动端(手机端)将会响应json格式的数据,如下二、Springboot异常处理为什么我们请求错误的路径,boot会给我们返......
  • Springboot整合jsp
    1.创建项目2.选择war工程3.这里可以选择web模块引入(我这里选择的boot版本是2.2.1)4.必须要引入的依赖<dependency><groupId>org.apache.tomcat.embed</groupId><artifa......
  • js是什么、html、css
    一:js简介1.前端三剑客:html+css+js,其中html是用来部署网页的结构,css是用来美化网页,而js则是让网页具有行为,让页面具有动态效果(动态不是简单指页面动起来,是后台与前台可以......
  • [RxJS] Ignore values during windows using throttleTime
    Bydefault,throttleTime(x),afterfirsteventemit,thenwaitforxamountoftime,thenemitanotherlatestvalue.Allthevaluesbetweenthewaitingtimewi......
  • 如何使用KrpanoToolJS在浏览器切图
    如何使用KrpanoToolJS在浏览器切图框架DEMO框架源码地址【独辟蹊径】逆推Krpano切图算法,实现在浏览器切多层级瓦片图一、功能介绍在浏览器中将全景图转为立方体图、......
  • nodejs md5 加盐值
    constcrypto=require('crypto')constSALT='ftmsabcd@1234!'functionmd5(str){constmd5=crypto.createHash('md5')constresult=md5.update(SALT).update(st......