zoukankan      html  css  js  c++  java
  • 获取系统时间差

    Vue中使用延时定时器,递归调用 countDown 函数,进行倒计时

    countDown(time) {
                if (time == 0) {
                    this.timer && clearTimeout(this.timer);
                    this.timeString = "结束了";
                    return;
                }
                const addZero = n => (n < 10 ? "0" + n : n);
                let diff = time - 1;
                let d = Math.floor(diff / 3600 / 24),
                    h = Math.floor((diff / 3600) % 24),
                    m = Math.floor((diff / 60) % 60),
                    s = Math.floor(diff % 60);
                d = addZero(d);
                h = addZero(h);
                m = addZero(m);
                s = addZero(s);
                this.timeString = `${d}天${h}时${m}分${s}秒`;
                // 递归调用
                this.timer = setTimeout(() => {
                    this.countDown(diff);
                }, 1000);
            }
    

    JavaScript 获取系统时间差,处理倒计时 天、时、分、秒

    let countDown = (time) => {
                if (time == 0) {
                    timer && clearTimeout(this.timer);
                    timeString = "结束了";// 要展示的时间字符串
                    return;
                }
                // 补0 格式化展示时间
                const addZero = n => (n < 10 ? "0" + n : n);
                let diff = time - 1;
                // 后台返回的是秒,进行天、小时、分钟、秒钟的时间转换
                let d = Math.floor(diff / 3600 / 24),
                    h = Math.floor((diff / 3600) % 24),
                    m = Math.floor((diff / 60) % 60),
                    s = Math.floor(diff % 60);
                d = addZero(d);
                h = addZero(h);
                m = addZero(m);
                s = addZero(s);
                // 字符串拼接
                this.timeString = `${d}天${h}时${m}分${s}秒`;
                // 递归调用
                this.timer = setTimeout(() => {
                    this.countDown(diff);
                }, 1000);
            }
    
    
  • 相关阅读:
    [PHP] 自定义错误处理
    [PHP] url的pathinfo模式加载不同控制器的实现
    [PHP] 自动加载的实现
    [javaSE] java获取文件列表
    [PHP] PHP请求Socket接口测试
    [PHP] java读取PHP接口数据
    [PHP] 读取大文件并显示
    [javaSE] java上传图片给PHP
    HDUOJ----Eddy's research I
    HDUOJ--8球胜负
  • 原文地址:https://www.cnblogs.com/zxk5211/p/web_33.html
Copyright © 2011-2022 走看看