zoukankan      html  css  js  c++  java
  • js简单倒计时

    不想每次用倒计时,都现写代码,比较烦,这里记一下,也顺便分享一些倒计时简单的逻辑。

    如果你有更简单方便的代码,可以分享给大家。

    var method = {
        countdownObj: {
            timer: null,
            changeTime: 0,
        },
        countdown: function(long, back) {
            var that = this;
            if (that.countdownObj.timer) {
                clearInterval(that.countdownObj.timer);
            }
            that.countdownObj.changeTime = long;
            back(that.countdownObj.changeTime);
            that.countdownObj.timer = setInterval(function() {
                that.countdownObj.changeTime--;
                back(that.countdownObj.changeTime);
                if (that.countdownObj.changeTime < 1) {
                    clearInterval(that.countdownObj.timer);
                }
            }, 1000);
        }
    };
    
    method.countdown(60,function(time){
        console.log(time);
    });
    

    函数里第一个数字是到时间长度,
    第二个回调函数,回传的time就是当前时间。

    勘误:
    1018-12-12 修正了几个文字错误;优化了几个变量

    原文地址:https://segmentfault.com/a/1190000017341822
  • 相关阅读:
    相关书籍下载2
    神奇的null和undefined
    相关书籍下载1
    微信小程序之for循环
    渐变(Gradients)
    模拟今日头条顶部导航菜单
    网格布局之相关特性
    网格布局之合并单元格
    网格布局
    Linux常用命令
  • 原文地址:https://www.cnblogs.com/lovellll/p/10109183.html
Copyright © 2011-2022 走看看