小程序上實現秒殺倒計時效果

在util中封裝方法

function countdown(that) {
    // 渲染倒計時時鐘
    // console.log(that.data.times)
    
    if (that.data.times <= 0) {
        // 結束倒計時
        that.setData({
            day: dateformats(0),
            hours: dateformat(0),
            minutes: dateformat_hur(0),
            seconds: dateformat_tim(0),
            ms: dateformat_fz(0)
        });
        return;
    }
    that.setData({
        day: dateformats(that.data.times),
        hours: dateformat(that.data.times),
        minutes: dateformat_hur(that.data.times),
        seconds: dateformat_tim(that.data.times),
        ms: dateformat_fz(that.data.times)
    });
    //settimeout實現倒計時效果
    setTimeout(function () {
        // 放在最後--
        that.data.times -= 10;
        countdown(that);
    }, 10) //注意毫秒的步長受限於系統的時間頻率,於是我們精確到0.01s即10ms
}
// 時間格式化輸出,如1天天23時時12分分12秒秒12 。每10ms都會調用一次
function dateformat(micro_second) {
    // 總秒數
    var second = Math.floor(micro_second / 1000);
    // 天數
    var day = 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章