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 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?