最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

定时器之计时(时间的转换)

互联网 admin 0浏览 0评论

定时器之计时(时间的转换)

###示例:
1.需求描述:
当点击开始按钮时开始计时,点击结束时停止计时。并将最后的时间转化为相应的00:00:00的格式
方案1:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="./vue.js"></script>
</head>
<body><div id="app"><span>{{callTime}}</span><button @click="handle">开始</button><button @click="ending">结束</button></div>
</body>
<script>var app=new Vue({el:"#app",data(){return {callTime:"00:00:00",timer:null,//定时器currentTime:0,//初始时间0}},methods:{secondToTimeStr (t) {t = Number(t)var a, i, e, time;if (!t) returnif (t < 60) return '00:00:' + ((i = t) < 10 ? '0' + i : i)if (t < 3600) return '00:' + ((a = parseInt(t / 60)) < 10 ? '0' + a : a) + ':' + ((i = t % 60) < 10 ? '0' + i : i)if (t >= 3600) {// var a, i, e = parseInt(t / 3600)return ((e = parseInt(t / 3600)) < 10 ? '0' + e : e) + ':' + ((a = parseInt(t % 3600 / 60)) < 10 ? '0' + a : a) + ':' + ((i = t % 60) < 10 ? '0' + i : i)}},handle(){if (!this.timer) {this.timer = setInterval(()=>{this.callTime = this.secondToTimeStr(++this.currentTime)}, 1000)}},ending(){this.timer && clearInterval(this.timer)}}})
</script>
</html>
在这里插入代码片

定时器之计时(时间的转换)

###示例:
1.需求描述:
当点击开始按钮时开始计时,点击结束时停止计时。并将最后的时间转化为相应的00:00:00的格式
方案1:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="./vue.js"></script>
</head>
<body><div id="app"><span>{{callTime}}</span><button @click="handle">开始</button><button @click="ending">结束</button></div>
</body>
<script>var app=new Vue({el:"#app",data(){return {callTime:"00:00:00",timer:null,//定时器currentTime:0,//初始时间0}},methods:{secondToTimeStr (t) {t = Number(t)var a, i, e, time;if (!t) returnif (t < 60) return '00:00:' + ((i = t) < 10 ? '0' + i : i)if (t < 3600) return '00:' + ((a = parseInt(t / 60)) < 10 ? '0' + a : a) + ':' + ((i = t % 60) < 10 ? '0' + i : i)if (t >= 3600) {// var a, i, e = parseInt(t / 3600)return ((e = parseInt(t / 3600)) < 10 ? '0' + e : e) + ':' + ((a = parseInt(t % 3600 / 60)) < 10 ? '0' + a : a) + ':' + ((i = t % 60) < 10 ? '0' + i : i)}},handle(){if (!this.timer) {this.timer = setInterval(()=>{this.callTime = this.secondToTimeStr(++this.currentTime)}, 1000)}},ending(){this.timer && clearInterval(this.timer)}}})
</script>
</html>
在这里插入代码片

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论