npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

v-timers

v0.0.9

Published

Mixin to manage your intervals in Vue.js

Downloads

12

Readme

v-timers

Simple mixin to manage timers or intervals for Vue.js

Latest Version on NPM Software License npm npm

Installation

1.1 Use CDN

<script src="https://cdn.jsdelivr.net/npm/v-timers/dist/v-timers.umd.js"></script>

1.2 Install from package manager

npm install v-timers
yarn add v-timers

2.1. Global import

import Vue from 'vue'
import VTimers from 'v-timers'

Vue.use(VTimers)

2.2. Or use mixin for the specific component

import {mixin as VTimers} from 'v-timers'
// -------- //
import { VTimerMixin } from 'v-timers'

export default {
  mixins: [VTimers],
  // ------ //
  mixins: [VTimerMixin],
}

2.3. Nuxt Module

nuxt.config.js:

export default {
  modules: [
    'v-timers/nuxt'
  ]
}

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 v-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 'v-timers'

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

App.vue

<template>
  <timer-component
    @timers:start:log="timerStarted"
    @timers:stop:log="timerStopped"
    @timers: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 'v-timers'

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

Author

Chantouch Sek

License

MIT