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

vue-plugin-timers

v0.1.6

Published

Interval and Timeout timers for Vue as a plugin or mixin

Downloads

644

Readme

vue-plugin-timers

Travis (.org) Codecov npm npm npm type definitions unpkg umd min:gzip size BCH compliance Total alerts Language grade: JavaScript Maintainability codebeat badge Codacy Badge

A Vue.JS plugin/mixin that helps set timeouts and heartbeats in your Vue components

The Problem - Creating Heartbeats

Say we create a vue.js component

export default {
  data: {
    minutes: new Date().getMinutes()
  }
}

What if we want minutes to update along with the user's clock when this component is open.

export default {
  get minutes() {
    return new Date().getMinutes()
  }
}

But, unfortunately this does not work. Reason ? new Date() or Date.now() type of objects are not reactive We get their value once, but it doesn't update every minutes/second or so

Solution - Use timers

With vue-plugin-timers we can create a timers option in Vue components. Timers run a method in your component every x seconds (where you can choose x).

How to Use ?

We can activate timers in two ways

  1. As a Component Mixin in any component

    // @/components/AwesomeComponent.vue <script>
    import { VueTimersMixin } from 'vue-plugin-timers'
    export default {
      mixins: [VueTimersMixin],
      ...,
      timers: { ... }
    }
  2. As a global Vue Plugin if you want to use in multiple components

    // @/main.js
    import Vue from 'vue'
    import VueTimers from 'vue-plugin-timers'
    Vue.use(VueTimers)

    You can use timers property in all components now

    // @/components/AnyComponent.vue <script>
    export default {
      ...,
      timers: { ... }
    }

You can define timers in 2 ways -

  1. As a timers property in component options (in single-file-component export or inside Vue.extend())
  2. If using vue-class-component, then there are @Timer decorators 🎉

Usage: timers component option

// @/components/AwesomeComponent.vue - <script>
export default {
  data: {
    time: new Date().toTimeString()
  },
  methods: {
    updateTime() {this.time = new Date().toTimeString()}
  },
  timers: {
    /* key = name of method */
    updateMinutes: {
      /* interval of delay (default: 1000) */
      interval: 30000,
      /* repeat (default: false)
      true => setInterval, 
      false => setTimeout once */
      repeat: true
    }
  }
}

Usage: @Timer decorator

⚠️ IMPORTANT: This needs vue-class-component to be installed, (or vue-property-decorator)

import Vue from 'vue'
import Component from 'vue-class-component'
import { Timer } from 'vue-plugin-timer'

@Component
class TimerComponent extends Vue {
    count = 0
    
    @Timer({ interval: 200 })
    incr() {
      this.count++
    }
}

The @Timer decorator takes all the same options as the timers component options hashes do. Except the method name, because you are decorating the method itself and so it is not needed

NOTE: The umd builds do NOT have the decorator, as decorators are something we usually use when building with Vue CLI using Babel and/or Typescript