Giter Site home page Giter Site logo

vue-timers's Introduction

vue-timers

Simple mixin to manage timers or intervals for Vue.js

Installation

1. Install from package manager

npm install vue-timers
yarn add vue-timers

2.1. Global import

import Vue from 'vue'
import VueTimers from 'vue-timers'

Vue.use(VueTimers)

2.2. Or use mixin for the specific component

import VueTimers from 'vue-timers/mixin'

export default {
  mixins: [VueTimers]
}

What it does?

It creates timer instances in components and slightly reduces boilerplate code with their handling.
See the following code

export default {
  methods: {
    log () {
      console.log('Hello world')
    }
  },
  created () {
    // It looks OK for the first look
    // But imagine that you have more than one timer
    this.$options.interval = setInterval(this.log, 1000)
    // Ok? What about check if timer works?
    // And it's not reactive so you should create data option
    console.log(this.$options.interval !== null)  
    // Still ok? So what about reusable timers?
    // New method for that? Rly?  
  },
  // Did you forget that it should be destroyed?
  beforeDestroy () {
    clearInterval(this.$options.interval)
  }
}

It's ugly, isn't it? So let's try to fix this :)

Same code with vue-timers:

export default {
  timers: {
    log: { time: 1000, autostart: true }
  },
  methods: {
    log () {
      console.log('Hello world')
    }
  }
}

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

this.timers.log.time = 2000

NOTE: you should restart timer to apply changes

Component methods

// Starts `log` timer
this.$timer.start('log')
// Stops `log` timer
this.$timer.stop('log')

isRunning property

this.timers.log.isRunning

Events

TimerComponent.vue

import { timer } from 'vue-timers'

export default {
  timers: [
    timer('log', 1000)
  ],
  methods: {
    log () {
      console.log('It works!')
    }
  }
}

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

export default {
  timers: {
    log: { time: 1000, ...options }
  }
}

Array notation

export default {
  timers: [
    { name: 'log' time: 1000, ...options }
  ]
}

Helper function

import { timer } from 'vue-timers'

export default {
  timers: [
    timer('log', 1000, { ...options })
  ]
}

Author

Anton Kosykh

License

MIT

vue-timers's People

Contributors

kelin2025 avatar masongzhi avatar wegenern avatar mcpower avatar jeremywohl avatar kalabasa avatar gregpeden avatar

Watchers

daniel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.