iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Vue-cli框架中怎么实现一个计时器应用
  • 388
分享到

Vue-cli框架中怎么实现一个计时器应用

2023-06-20 20:06:48 388人浏览 薄情痞子
摘要

Vue-cli框架中怎么实现一个计时器应用,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。使用说明开始正计时:点击工具栏的“开始正计时”按钮即可,快捷键为"Enter&

Vue-cli框架中怎么实现一个计时器应用,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

使用说明

开始正计时:点击工具栏的“开始正计时”按钮即可,快捷键为"Enter"键。

开始倒计时:在输入框内写入时间后,点击“开始倒计时”按钮,即可开始倒计时。

暂停计时器:点击“暂停计时器”按钮即可暂停。

清空正/倒计时:点击此按钮,计时器便会回到初始状态,等待新的计时。

重新再计时:点击此按钮,计时器便会重新开始此次计时。

恢复计时器:点击此按钮即可从暂停状态中恢复。

Vue-cli框架中怎么实现一个计时器应用

代码

首先初始化项目

vue init webpack <project name>

components文件夹中放入文件:CounterButton.vue

<template>  <div>    <button v-bind:class="styleObject" v-on:click="$emit('click-button')">{{ text }}</button>  </div></template><script>export default {  name: "CounterButton",  props: {    text: String  },  data: function() {    return {      styleObject: {        countup: false,        countdown: false,        clear: false,        pause: false,        restart: false,        resume: false      }    };  },  created: function() {    if (this.text == "开始正计时") {      this.styleObject.countup = true;    } else if (this.text == "开始倒计时") {      this.styleObject.countdown = true;    } else if (this.text == "清空倒计时" || this.text == "清空正计时") {      this.styleObject.clear = true;    } else if (this.text == "暂停计时器") {      this.styleObject.pause = true;    } else if (this.text == "重新再计时") {      this.styleObject.restart = true;    } else if (this.text == "恢复计时器") {      this.styleObject.resume = true;    }  }};</script><style>.countup {  background-color: #2188e9;  border-radius: 5px;  border-color: #2188e9;  position: absolute;  left: 310px;  top: 15px;  width: 98px;  height: 40px;  font-family: PingFangSC-Regular, "PingFang SC", sans-serif;  font-size: 16px;  color: #ffffff;}.countdown {  background-color: #2188e9;  border-radius: 5px;  border-color: #2188e9;  position: absolute;  left: 428px;  top: 15px;  width: 98px;  height: 40px;  font-family: PingFangSC-Regular, "PingFang SC", sans-serif;  font-size: 16px;  color: #ffffff;}.clear {  background-color: #dd2e1d;  border-radius: 5px;  border-color: #dd2e1d;  position: absolute;  left: 964px;  top: 15px;  width: 98px;  height: 40px;  font-family: PingFangSC-Regular, "PingFang SC", sans-serif;  font-size: 16px;  color: #ffffff;}.pause {  background-color: #2188e9;  border-radius: 5px;  border-color: #2188e9;  position: absolute;  left: 227px;  top: 15px;  width: 98px;  height: 40px;  font-family: PingFangSC-Regular, "PingFang SC", sans-serif;  font-size: 16px;  color: #ffffff;}.restart {  background-color: #ffb020;  border-radius: 5px;  border-color: #ffb020;  position: absolute;  left: 1082px;  top: 15px;  width: 98px;  height: 40px;  font-family: PingFangSC-Regular, "PingFang SC", sans-serif;  font-size: 16px;  color: #ffffff;}.resume {  background-color: #2188e9;  border-radius: 5px;  border-color: #2188e9;  position: absolute;  left: 227px;  top: 15px;  width: 98px;  height: 40px;  font-family: PingFangSC-Regular, "PingFang SC", sans-serif;  font-size: 16px;  color: #ffffff;}</style>

将App.vue改为

<template>  <div id="app">    <div class="toolbar">      <div v-show="initialSeen">        <input v-model="hour" id="hour" />        <input v-model="minute" id="minute" />        <input v-model="second" id="second" />        <span id="hourlabel">时</span>        <span id="minutelabel">分</span>        <span id="secondlabel">秒</span>        <counter-button text="开始正计时" v-on:click-button="startCountUp" id="countup"></counter-button>        <counter-button text="开始倒计时" v-on:click-button="startCountDown" id="countdown"></counter-button>      </div>      <span id="hint" v-show="hintSeen">{{ hint }}</span>      <counter-button v-bind:text="clearText" v-on:click-button="clearCounter" v-show="clearSeen" id="clear"></counter-button>      <counter-button text="暂停计时器" v-on:click-button="pauseCounter" v-show="pauseSeen" id="pause"></counter-button>      <counter-button text="重新再计时" v-on:click-button="restartCounter" v-show="restartSeen" id="restart"></counter-button>      <counter-button text="恢复计时器" v-on:click-button="resumeCounter" v-show="resumeSeen" id="resume"></counter-button>    </div>    <span id="time">{{ time }}</span>  </div></template><script>import CounterButton from "./components/CounterButton";export default {  name: "App",  data: function() {    return {      status: 1,      // status---1: before start; 2: up timing; 3: down timing; 4: up pausing;      // 5: down pausing; 6: down finished;      hour: null,      minute: null,      second: null,      time: "00:00:00",      timer: null,      Hour: 0,      Minute: 0,      Second: 0,      Millisecond: 0,      hourString: "",      minuteString: "",      secondString: "",      recordHour: 0,      recordMinute: 0,      recordSecond: 0,      recordMillisecond: 0,      hint: "正在倒计时 12:20:00",      clearText: "清空倒计时",      initialSeen: true,      clearSeen: false,      pauseSeen: false,      restartSeen: false,      resumeSeen: false,      hintSeen: false    };  },  methods: {    fORMat: function(Hour, Minute, Second) {      if (Second < 10) {        this.secondString = "0" + Second;      } else {        this.secondString = Second;      }      if (Minute < 10) {        this.minuteString = "0" + Minute;      } else {        this.minuteString = Minute;      }      if (Hour < 10) {        this.hourString = "0" + Hour;      } else {        this.hourString = Hour;      }      return (        this.hourString + ":" + this.minuteString + ":" + this.secondString      );    },    changeStatus: function(aimStatus) {      if (aimStatus == 1) {        // before start        this.initialSeen = true;        this.clearSeen = false;        this.pauseSeen = false;        this.restartSeen = false;        this.resumeSeen = false;        this.hintSeen = false;      } else if (aimStatus == 2 || aimStatus == 3) {        // up timing || down timing        this.initialSeen = false;        this.clearSeen = true;        this.pauseSeen = true;        this.restartSeen = true;        this.resumeSeen = false;        this.hintSeen = true;        if (aimStatus == 2) {          this.hint = "正在正计时";          this.clearText = "清空正计时";        } else if (aimStatus == 3) {          this.recordHour = parseInt(this.recordMillisecond / 3600000);          this.recordMinute = parseInt(            (this.recordMillisecond % 3600000) / 60000          );          this.recordSecond = parseInt((this.recordMillisecond % 60000) / 1000);          this.hint =            "正在倒计时 " +            this.format(this.recordHour, this.recordMinute, this.recordSecond);          this.clearText = "清空倒计时";        }      } else if (aimStatus == 4 || aimStatus == 5) {        // up pausing || down pausing        this.initialSeen = false;        this.clearSeen = true;        this.pauseSeen = false;        this.restartSeen = true;        this.resumeSeen = true;        this.hintSeen = true;        if (aimStatus == 4) {          // up pausing          this.hint = "暂停正计时";          this.clearText = "清空正计时";        } else if (aimStatus == 5) {          // down pausing          this.recordHour = parseInt(this.recordMillisecond / 3600000);          this.recordMinute = parseInt(            (this.recordMillisecond % 3600000) / 60000          );          this.recordSecond = parseInt((this.recordMillisecond % 60000) / 1000);          this.hint =            "暂停倒计时 " +            this.format(this.recordHour, this.recordMinute, this.recordSecond);          this.clearText = "清空倒计时";        }      } else if (aimStatus == 6) {        // down finished        this.initialSeen = false;        this.clearSeen = true;        this.pauseSeen = false;        this.restartSeen = true;        this.resumeSeen = false;        this.hintSeen = true;        this.recordHour = parseInt(this.recordMillisecond / 3600000);        this.recordMinute = parseInt(          (this.recordMillisecond % 3600000) / 60000        );        this.recordSecond = parseInt((this.recordMillisecond % 60000) / 1000);        this.hint =          "倒计时 " +          this.format(this.recordHour, this.recordMinute, this.recordSecond) +          " 已结束";      }    },    CountUp: function() {      this.Millisecond += 50;      this.Hour = parseInt(this.Millisecond / 3600000);      this.Minute = parseInt((this.Millisecond % 3600000) / 60000);      this.Second = parseInt((this.Millisecond % 60000) / 1000);      this.time = this.format(this.Hour, this.Minute, this.Second);    },    CountDown: function() {      this.Millisecond -= 50;      this.Hour = parseInt(this.Millisecond / 3600000);      this.Minute = parseInt((this.Millisecond % 3600000) / 60000);      this.Second = parseInt((this.Millisecond % 60000) / 1000);      if (this.Millisecond <= 0) {        clearInterval(this.timer);        this.changeStatus(6);      }      this.time = this.format(this.Hour, this.Minute, this.Second);    },    startCountUp: function() {      this.status = 2;      this.Millisecond = 0;      this.changeStatus(this.status);      this.timer = setInterval(this.CountUp, 50);    },    startCountDown: function() {      this.status = 3;      this.Hour = this.hour;      if (this.minute > 59) {        this.Minute = 59;      } else {        this.Minute = this.minute;      }      if (this.second > 59) {        this.Second = 59;      } else {        this.Second = this.second;      }      this.hour = null;      this.minute = null;      this.second = null;      this.Millisecond =        this.Hour * 3600000 + this.Minute * 60000 + this.Second * 1000;      this.recordMillisecond = this.Millisecond;      this.changeStatus(this.status);      this.timer = setInterval(this.CountDown, 50);    },    clearCounter: function() {      this.status = 1;      this.changeStatus(this.status);      clearInterval(this.timer);      this.time = this.format(0, 0, 0);    },    pauseCounter: function() {      if (this.status == 2) {        // Now count up        this.status = 4;        this.changeStatus(this.status);        clearInterval(this.timer);      } else if (this.status == 3) {        // now count down        this.status = 5;        this.changeStatus(this.status);        clearInterval(this.timer);      }    },    restartCounter: function() {      if (this.status == 2 || this.status == 4) {        this.status = 2;        this.Millisecond = 0;        this.changeStatus(this.status);        clearInterval(this.timer);        this.timer = setInterval(this.CountUp, 50);      } else if ((this.status = 3 || this.status == 5 || this.status == 6)) {        this.status = 3;        this.Millisecond = this.recordMillisecond;        this.changeStatus(this.status);        clearInterval(this.timer);        this.timer = setInterval(this.CountDown, 50);      }    },    resumeCounter: function() {      if (this.status == 4) {        this.status = 2;        this.changeStatus(this.status);        this.timer = setInterval(this.CountUp, 50);      } else if ((status = 5)) {        this.status = 3;        this.changeStatus(this.status);        this.timer = setInterval(this.CountDown, 50);      }    },    // 键盘事件    handleKeyup(event) {      const e = event || window.event || arguments.callee.caller.arguments[0];      if (!e) return;      const { key, keyCode } = e;      if (key == "Enter") {        if (this.status == 1) {          // before start          this.status = 2;          this.Millisecond = 0;          this.changeStatus(this.status);          this.timer = setInterval(this.CountUp, 50);        }      } else if (keyCode == 32) {        if (this.status == 2) {          // Now count up          this.status = 4;          this.changeStatus(this.status);          clearInterval(this.timer);        } else if (this.status == 3) {          // now count down          this.status = 5;          this.changeStatus(this.status);          clearInterval(this.timer);        } else if (this.status == 4) {          this.status = 2;          this.changeStatus(this.status);          this.timer = setInterval(this.CountUp, 50);        } else if (this.status == 5) {          this.status = 3;          this.changeStatus(this.status);          this.timer = setInterval(this.CountDown, 50);        }      }    }  },  mounted: function() {    window.addEventListener("keyup", this.handleKeyup);  },  destroyed() {    window.removeEventListener("keyup", this.handleKeyup);  },  components: {    CounterButton  }};</script><style>body {  margin: 0;  padding: 0;}.toolbar {  background-color: #97a5bc;  width: 1220px;  height: 70px;}#hour {  background-color: white;  border-radius: 5px;  position: absolute;  left: 40px;  top: 15px;  width: 69px;  height: 34px;  font-size: 15px;}#hourlabel {  position: absolute;  left: 86px;  top: 24px;  font-family: PingFangSC-Regular, "PingFang SC", sans-serif;  font-size: 16px;  color: #222222;}#minute {  background-color: white;  border-radius: 5px;  position: absolute;  left: 130px;  top: 15px;  width: 69px;  height: 34px;  font-size: 15px;}#minutelabel {  position: absolute;  left: 177px;  top: 24px;  font-family: PingFangSC-Regular, "PingFang SC", sans-serif;  font-size: 16px;  color: #222222;}#second {  background-color: white;  border-radius: 5px;  position: absolute;  left: 220px;  top: 15px;  width: 69px;  height: 34px;  font-size: 15px;}#secondlabel {  position: absolute;  left: 268px;  top: 24px;  font-family: PingFangSC-Regular, "PingFang SC", sans-serif;  font-size: 16px;  color: #222222;}#time {  position: absolute;  left: 131px;  top: 197px;  font-size: 200px;  font-family: PTMono-Bold, "PT Mono", monospace;  font-weight: 700;  color: #333333;}#hint {  position: absolute;  left: 40px;  top: 24px;  font-family: PTMono-Bold, "PT Mono", monospace;  font-size: 16px;  color: white;}</style>

最后在目录中使用

npm run dev

看完上述内容,你们掌握Vue-cli框架中怎么实现一个计时器应用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程网精选频道,感谢各位的阅读!

--结束END--

本文标题: Vue-cli框架中怎么实现一个计时器应用

本文链接: https://www.lsjlt.com/news/299856.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Vue-cli框架中怎么实现一个计时器应用
    Vue-cli框架中怎么实现一个计时器应用,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。使用说明开始正计时:点击工具栏的“开始正计时”按钮即可,快捷键为"Enter&...
    99+
    2023-06-20
  • Vue-cli框架实现计时器应用
    技术背景 本应用使用 vue-cli 框架,并使用了自定义组件(将按钮拆分成单独的组件)和vue 模板。 使用说明 开始正计时:点击工具栏的“开始正计时”按钮即可,快捷键为"Ente...
    99+
    2024-04-02
  • vue-cli 3中怎么实现一个全局过滤器
    vue-cli 3中怎么实现一个全局过滤器,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。1、创建 filters.js首先新建一个filter...
    99+
    2024-04-02
  • JavaScript中怎么实现一个计时器
    这期内容当中小编将会给大家带来有关JavaScript中怎么实现一个计时器,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。window.setInterval(); 这个方...
    99+
    2024-04-02
  • 使用vue怎么实现一个倒计时功能
    这期内容当中小编将会给大家带来有关使用vue怎么实现一个倒计时功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。通过父组件传入的结束时间减去当前日期得到剩余时间子组件部分<div clas...
    99+
    2023-06-14
  • Node.js中怎么实现一个express框架
    本篇文章给大家分享的是有关Node.js中怎么实现一个express框架,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。express的基本用法...
    99+
    2024-04-02
  • 使用Vue怎么创建一个vue-cli脚手架程序
    使用Vue怎么创建一个vue-cli脚手架程序?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。1. Vue--第一个vue-cli程序Vue的开发都是要基于Nod...
    99+
    2023-06-15
  • linux中怎么利用timerfd_create实现一个计时器
    linux中怎么利用timerfd_create实现一个计时器,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。timer_poll.h代码如下:#ifndef T...
    99+
    2023-06-09
  • 怎么在Python中实现一个WSGI框架
    怎么在Python中实现一个WSGI框架?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。python有哪些常用库python常用的库:1.requesuts;2.scrapy;3...
    99+
    2023-06-14
  • JavaScript中怎么实现一个倒数计时器
    本篇文章给大家分享的是有关JavaScript中怎么实现一个倒数计时器,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。有时,您将需要构建一个Ja...
    99+
    2024-04-02
  • 使用Java怎么实现一个RPC框架
    使用Java怎么实现一个RPC框架?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、RPC简介RPC,全称为Remote Procedure Call,即远程过程调用,它是一个...
    99+
    2023-05-30
    java rpc
  • vue-cli中怎么利用webpack 构建一个多页面应用
    vue-cli中怎么利用webpack 构建一个多页面应用,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。关于vue.jsvue.js是一套...
    99+
    2024-04-02
  • python实现一个简单的web应用框架
    目录引言写应用框架需要写底层服务器么uwsgi基本使用安装uwsgi配置uwsgiuwsgi常用配置uwsgi启服和停服启动一个demo写一个简单的web应用框架总结引言 本篇文章所...
    99+
    2023-05-18
    python web应用框架 python web
  • Android应用中怎么实现一个验证码倒计时功能
    Android应用中怎么实现一个验证码倒计时功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。实现发送验证码的时候一般都会有一个按钮,点击之后便会给你输入的手机发送一条验证码,...
    99+
    2023-05-31
    android roi
  • 怎么在Android应用中利用View实现一个倒计时功能
    这篇文章将为大家详细讲解有关怎么在Android应用中利用View实现一个倒计时功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。Android 自定义View实现倒计时需求:具体方法如下:...
    99+
    2023-05-31
    android roi view
  • php7怎么实现一个简易框架
    本篇内容主要讲解“php7怎么实现一个简易框架”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“php7怎么实现一个简易框架”吧!框架的核心链路是从开始的请求路由解析到控制器的分发,model的数据...
    99+
    2023-06-20
  • vue中怎么实现一个模态框组件
    这期内容当中小编将会给大家带来有关vue中怎么实现一个模态框组件,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。组件结构<template> &nbs...
    99+
    2024-04-02
  • 使用CocosCreator怎么实现一个计时器功能
    这篇文章给大家介绍使用CocosCreator怎么实现一个计时器功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。一、setTimeOut3秒后打印abc。只执行一次。setTimeout(()=>{consol...
    99+
    2023-06-14
  • Python中怎么实现一个遗传算法框架
    本篇文章给大家分享的是有关Python中怎么实现一个遗传算法框架,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。算法特点以决策变量的编码作为运算对象,使得优化过程借鉴生物学中的概...
    99+
    2023-06-17
  • Android中怎么实现一个倒计时效果
    今天就跟大家聊聊有关Android中怎么实现一个倒计时效果,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。需求:a.在后台添加时,如果是今日直播,则需要添加开始时间(精确到秒);b.离...
    99+
    2023-05-30
    android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作