itanℒ 2019-07-31 10:23 采纳率: 64.1%
浏览 357

封装的时间函数怎么实现倒计时的功能.....

请问我这个封装的时间函数怎么实现倒计时的功能?

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>时间戳转化函数</title>
</head>

<body>
</body>
<script type="text/javascript">
    //日期格式化年月日时分秒函数
    var formatTimeToDate = function (time) {
        return new Date(time).format("yyyy-MM-dd hh:mm:ss");
    };
    //日期格式化年月日时
    var formatTimeToDay = function (time) {
        return new Date(time).format("yyyy-MM-dd");
    };
    Date.prototype.format = function (format) {
        var date = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3),
            "S+": this.getMilliseconds()
        };
        if (/(y+)/i.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (var k in date) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1
                    ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
            }
        }
        return format;
    };

    console.log(formatTimeToDate(1532156400000)) 
    console.log(formatTimeToDay(1532156400000))
</script>

</html>
  • 写回答

1条回答 默认 最新

  • 拾年丶不久 2019-07-31 10:26
    关注

    JS有setInterval方法,就是用来做计时器的

    var time = 5;//计时器计时次数,随便写了个5
    var timer = setInterval(function(){
    time --;
    if(time > 0){
    //这里写你要执行的方法
    }else{
    clearInterval(timer);
    }
    },1000)
    

    我这是一秒一计时,倒计时5秒的方法

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器