不想每次用倒计时,都现写代码,比较烦,这里记一下,也趁便分享一些倒计时简朴的逻辑。
假如你有更简朴轻易的代码,能够分享给人人。
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 修改了几个笔墨毛病;优化了几个变量