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?
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:
|
<<: Docker uses nextcloud to build a private Baidu cloud disk
>>: Installation, configuration and uninstallation of MySQL 8.0 in Windows environment
This article uses an example to illustrate the ex...
There is a medicine for regret in the world, as l...
Table of contents Preface 1. Props, $emit one-way...
1. First, you need to use the target selector of ...
This article example shares the specific code of ...
<div class="box"> <img /> &...
Table of contents Preface advantage: shortcoming:...
1. z-index is invalid in IE6. In CSS, the z-index...
Menu bar example 1: Copy code The code is as foll...
There are many tags in XHTML, but only a few are ...
Table of contents Preface Conversion relationship...
Table of contents 1. Vue3 component communication...
NC's full name is Netcat (Network Knife), and...
Preview versions of Safari (Technology Preview 10...
Error occurs: When exporting the database from My...