不想每次用倒計時,都現寫代碼,比較煩,這裏記一下,也順便分享一些倒計時簡單的邏輯。函數
若是你有更簡單方便的代碼,能夠分享給你們。優化
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就是當前時間。this
勘誤:
1018-12-12 修正了幾個文字錯誤;優化了幾個變量code