vue-timers
Simple mixin to manage timers or intervals for Vue.js
Installation
1.1 Use CDN
<script src="https://cdn.jsdelivr.net/npm/vue-timers/dist/vue-timers.umd.js"></script>
1.2 Install from package manager
npm install vue-timers
yarn add vue-timers
2.1. Global import
Vue
2.2. Or use mixin for the specific component
mixins: VueTimers
2.3. Nuxt plugins
nuxt.config.js
:
plugins: src: '~/plugins/vue-timers' mode: 'client'
plugins/vue-timers.js
:
Vue
What it does?
It creates timer instances in components and slightly reduces boilerplate code with their handling.
See the following code
methods: { console } { // It looks OK for the first look // But imagine that you have more than one timer this$optionsinterval = // Ok? What about check if timer works? // And it's not reactive so you should create data option console // Still ok? So what about reusable timers? // New method for that? Rly? } // Did you forget that it should be destroyed? { }
It's ugly, isn't it? So let's try to fix this :)
Same code with vue-timers
:
timers: log: time: 1000 autostart: true methods: { console }
Configuration
Timer object
// Name of timer // Default: timer key (with object notation) name: String // Tick callback or method name from component // Note: callback is binded to component instance // Default: name callback: Function/String // Autostart timer from created hook // Default: false autostart: Boolean // Set true to repeat (with setInterval) or false (setTimeout) // Default: false repeat: Boolean // Set true to call first tick immediate // Note: repeat must be true too // Default: false immediate: Boolean // Time between ticks // Default: 1000 time: Number // Switch timer`s status between activated and deactivated // Default: false isSwitchTab: Boolean
Changing timer duration
thistimerslogtime = 2000
NOTE: you should restart timer to apply changes
Component methods
// Starts `log` timerthis$timerstart'log'// Stops `log` timerthis$timer
isRunning property
thistimerslogisRunning
Events
TimerComponent.vue
timers: methods: { console }
App.vue
<template> <timer-component @timer-start:log="timerStarted" @timer-stop:log="timerStopped" @timer-tick:log="timerTicked" /></template>
3 ways of timers declaration
Object notation
timers: log: time: 1000 ...options
Array notation
timers: name: 'log' time: 1000 ...options
Helper function
timers:
Author
License
MIT