Vue毫秒计时器(开始计时)

话不多说,直接上代码即可

<template>
	<view class="content">
		<button @click="start">开始</button>
		<view class="">
			{{time_filter?time_filter:''}}
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				time_num: 0
			}
		},
		onLoad(option) {

		},
		computed: {
			// 定时器拆分
			time_filter() {
				// var time_num = this.time_num * 50;
				var time_num = this.time_num * 50
				let hour = Math.floor(time_num / 1000 / 60 / 60) < 10 ? '0' + Math.floor(time_num / 1000 / 60 / 60) : Math
					.floor(time_num / 1000 / 60 / 60)
				let minute = Math.floor(time_num / 1000 / 60 % 60) < 10 ? '0' + Math.floor(time_num / 1000 / 60 % 60) :
					Math.floor(time_num / 1000 / 60 % 60);
				let second = Math.floor(time_num / 1000 % 60) < 10 ? '0' + Math.floor(time_num / 1000 % 60) : Math.floor(
					time_num / 1000 % 60);
				let m_second = (time_num % 1000).toString().slice(0, 2)
				let str = (hour == '00' ? '' : hour + 'h' + " ") + (minute == '00' ? '' : minute + 'min' + " ") + second +
					"." +
					m_second + 's'
					console.log(str);
				return str
			}
		},
		methods: {
			// 定时器
			start() {
				this.timer = setInterval(_ => {
					this.time_num += 1
				}, 50)
			},
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

</style>

效果图 以50毫秒累加

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值