uni-app

uni-app之定时器

2020-12-08  本文已影响0人  Cute_小肥鸡

setInterval()

<template>
</template>

<script>
    export default {
        data() {
            return {
                ExamTimeText: '' //考试时间
            }
        },
        onLoad(e) {
            /////////////定时器
            var my_self = this;
            var hour, minute, second; //时 分 秒
            hour = minute = second = 0; //初始化
            var millisecond = 0; //毫秒
            this.timer = setInterval(() => {
                millisecond = millisecond + 50;
                if (millisecond >= 1000) {
                    millisecond = 0;
                    second = second + 1;
                }
                if (second >= 60) {
                    second = 0;
                    minute = minute + 1;
                }
                if (minute >= 60) {
                    minute = 0;
                    hour = hour + 1;
                }

                //时
                if (hour < 10) {
                    var hour_1 = '0' + hour;
                } else {
                    var hour_1 = hour;
                }
                //分
                if (minute < 10) {
                    var minute_1 = '0' + minute;
                } else {
                    var minute_1 = minute;
                }
                //秒
                if (second < 10) {
                    var second_1 = '0' + second;
                } else {
                    var second_1 = second;
                }
                //将时分秒组成文本
                my_self.ExamTimeText = hour_1 + '时' + minute_1 + '分' + second_1 + '秒';
            }, 50);
        },
        methods: {
        
        }
    }
</script>

<style>
</style>

效果图
上一篇下一篇

猜你喜欢

热点阅读