微信小程序開發(fā)秒表計時器

wxml:

    <text class='marright'>{{hours}}:{{minute}}:{{second}}</text>

js:

page({
    data: {
        hours: '0' + 0,   // 時
        minute: '0' + 0,   // 分
        second: '0' + 0    // 秒
    },
        
    onLoad: function (options) {
        // 調(diào)用函數(shù)
        this.setInterval()
    }, 
    
    // 計時器
    setInterval: function () {
        const that = this
        var second = that.data.second
        var minute = that.data.minute
        var hours = that.data.hours       
        setInterval(function () {  // 設(shè)置定時器
            second++
            if (second >= 60) {
                second = 0  //  大于等于60秒歸零
                minute++
                if (minute >= 60) {
                    minute = 0  //  大于等于60分歸零
                    hours++
                    if (hours < 10) {
                        // 少于10補(bǔ)零
                        that.setData({
                            hours: '0' + hours
                        })
                    } else {
                        that.setData({
                            hours: hours
                        })
                    }
                }
                if (minute < 10) {
                    // 少于10補(bǔ)零
                    that.setData({
                        minute: '0' + minute
                    })
                } else {
                    that.setData({
                        minute: minute
                    })
                }
            }
            if (second < 10) {
                // 少于10補(bǔ)零
                that.setData({
                    second: '0' + second
                })
            } else {
                that.setData({
                    second: second
                })
            }
        }, 1000)
    },
})
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容