JavaScript源生手写高考倒计时分享

let strArr = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"];
        function init(dateTime, element,fontSize) {
            let times = ((new Date(dateTime)).getTime() - (new Date()).getTime()) / 1000;
            let day = getYear(Math.floor(times / 24 / 60 / 60), true);
            let h = getTime(Math.floor((times / 60 / 60) % 24));
            let m = getTime(Math.floor((times / 60) % 60), true);
            let s = getTime(Math.floor(times % 60), true);
            element.innerHTML = day + '天' + h + '小时' + m + '分钟' + s + '秒';
            element.style.color = `#${Math.floor(Math.random() * 0x1000000.toString(16))}`;
            element.style.fontSize = fontSize;
            element.style.fontWeight = "700";
        }
        function getYear(num, bool) {
            if (num > 100) return strArr[Math.floor(num / 100)] + '百' + strArr[Math.floor(num % 100 / 10)] + '十' + strArr[Math.floor(num % 10)];
            if (num > 10) return strArr[Math.floor(num % 100 / 10)] + '十' + strArr[Math.floor(num % 10)];
            if (num < 10) return strArr[Math.floor(num % 10)];
        }
        function getTime(num, bool) {
            if (num > 100) return;
            if (num === 100) return '一百';
            if (num < 10) return bool ? "零" + strArr[num] : strArr[num];
            if (num === 10) return "十";
            if (num % 10 === 0) return strArr[num / 10] + "十";
            if (num < 20) return "十" + strArr[num % 10];
            return strArr[Math.floor(num / 10)] + "十" + strArr[num % 10];
        }
        function fn(aaa, element,fontSize) {
            init(aaa, element,fontSize);
            setInterval(function () {
                init(aaa, element,fontSize);
            }, 1000);
        }

        let element = document.querySelector('p');
        fn('2021,6,7', element,'20px');

你可能感兴趣的:(JavaScript小dome)