Vue implements a simple timer component

Vue implements a simple timer component

When doing a project, it is inevitable to encounter requirements such as real-time refresh, advertisement animations appearing in sequence, etc. Recently, based on business needs, it is necessary to implement a timer for accumulating call duration. At this time, the timer is needed to enter our code stage. In fact, for the timer, its principle is to be realized through the timer. So before writing business requirements, let me talk about some knowledge about timers.

The window object provides two methods to implement the timer effect, namely window.setTimeout() and window.setInterval.

In Javascript, code is generally executed synchronously, but timers are executed asynchronously.

window.setTimeout(callback,delay); //callback: callback function delay: time interval window.setInterval(callback,delay);

Timers are divided into interval timer setInterval and delay timer setTimeout

So what is the difference between the two?

  • setInterval is executed in a cycle with a specified time period. It is generally used to refresh forms and execute complex animations in a cycle. It is also used to refresh and synchronize some forms at a specified time in real time.
  • setTimeout is only executed once after a specified time. For example, some websites will display a pop-up advertisement when you first enter them. Generally, setTimeout is used.

After understanding the basic knowledge of timers, you can then implement the functions.

HTML

<template>
    <div class="timer">
    <div>{{nowTime}}</div>
    </div>
</template>

Javascript

<script>
    export default {
    name: 'Timer',
    data () {
     return {
      timer: null,
      nowTime:"",
      hour: 0,
      minutes: 0,
      seconds: 0
      }
    },
    created () {
    this.timer = setInterval(this.startTimer, 1000);
    },
    destroyed () {
    clearInterval(this.timer);
    },
    
    methods: {
    startTimer () {
     //It is recommended to clear the timer before starting it to avoid unexpected bugs caused by timer accumulation.
     if(this.timer) {
   clearInterval(this.timer);
  }
     this.seconds += 1;
     if (this.seconds >= 60) {
      this.seconds = 0;
      this.minutes = this.minutes + 1;
     }
     if (this.minutes>= 60) {
      this.minutes = 0;
      this.hour = this.hour + 1;
     }
     this.nowTime = this.toZero(this.hour): this.toZero(this.minutes): this.toZero(this.seconds)
    },
     toZero(timeNumber) {
     return timeNumber<10?"0"+timeNumber:timeNumber
   },
 }
}
</script>

In this way, a simple timer component is implemented. In fact, there are other implementation ideas. If you encounter similar requirements in future development, you can refer to them. I hope it will be helpful to you.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Implementing a simple timer based on Vue method
  • Vue-cli framework implements timer application
  • Using Vue to implement timer function
  • Vue.js implements simple timer function
  • How to implement Vue timer
  • Detailed usage of Vue timer
  • Solve the problem of timer continuing to execute after Vue component is destroyed
  • Vue example code using timer to achieve marquee effect
  • Implementation code of vue timer component
  • How to encapsulate timer components in Vue3

<<:  Docker uses nextcloud to build a private Baidu cloud disk

>>:  Installation, configuration and uninstallation of MySQL 8.0 in Windows environment

Recommend

Getting Started Guide to MySQL Sharding

Preface Relational databases are more likely to b...

How to configure virtual user login in vsftpd

yum install vsftpd [root@localhost etc]# yum -y i...

Two-hour introductory Docker tutorial

Table of contents 1.0 Introduction 2.0 Docker Ins...

CSS to implement QQ browser functions

Code Knowledge Points 1. Combine fullpage.js to a...

MySQL 5.6 binary installation process under Linux

1.1 Download the binary installation package wget...

Vue scroll down to load more data scroll case detailed explanation

vue-infinite-scroll Install npm install vue-infin...

Markup Language - Anchor

Previous: Markup Language - Phrase Elements Origin...

Detailed explanation of Vuex overall case

Table of contents 1. Introduction 2. Advantages 3...

Vue local component data sharing Vue.observable() usage

As components become more detailed, you will enco...

How to view and modify the time zone in MySQL

Today I found that a program inserted an incorrec...

Detailed explanation of the basic usage of the Linux debugger GDB

Table of contents 1. Overview 2. gdb debugging 2....

Things to note when migrating MySQL to 8.0 (summary)

Password Mode PDO::__construct(): The server requ...

CSS realizes the layout method of fixed left and adaptive right

1. Floating layout 1. Let the fixed width div flo...

Native js to implement 2048 game

2048 mini game, for your reference, the specific ...