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

@xesam/timer

v0.1.0

Published

a simple javascript timer wrapper

Downloads

9

Readme

Timer.js

一个简单的 Javascript 计时器封装(A simple javascript Timer);增加了一些控制方法:

start(timeout) // 开启计时器
pause() // 暂停计时器
resume() // 恢复计时器
stop() // 停止计时器

install

    npm install @xesam/timer

普通计时器(Timer)

使用方法:

const timer = new Timer(interval, eventHandleFunction); // eventHandler 会接收到一个 event 事件
timer.on('tick', () => {
});
timer.start();
timer.pause();
timer.resume();
timer.stop();

默认的 event 事件类型:

start
pause
resume
stop
tick

比如每次 tick 都会收到 tick 事件,而 start, pause, resume, stop 则会收到 start,pause,resume,stop的事件。

const timer = new Timer(1000);
timer.on('start', console.log);
timer.on('stop', console.log);
timer.on('pause', console.log);
timer.on('resume', console.log);
timer.on('tick', console.log);

定时器(CounterTimer)

使用方法:

const timer = new CounterTimer(interval);
timer.on('tick', () => {
    console.log(timer.getCount());
});
timer.start();
timer.pause();
timer.resume();
timer.stop();

相比 Timer,CounterTimer 增加了 getCount() 方法,用来获取计数器的值。每次 tick 都会增加 count 的值。

可以指定最大的count值,在到达最大的 count 之后,会触发 done 事件:

const timer = new CounterTimer(interval, 3);
timer.on('done', () => {
    console.log(timer.getCount()); // `3`
});
timer.start();

元素定时发射(EmitterTimer)

将数组元素定时发射出去的计时器, tick 参数是 {data, index}:

index : emit 元素的数组索引
data : index 对应的数组元素
const timer = new EmitterTimer(1000);
timer.on('tick', ({ data, index }) => {
    console.log(index, data);
});
timer.start();

倒计时计时器(CountdownTimer)

使用方法:

const timer = new CountdownTimer(1000, 10000);
timer.on('tick', ({ leftMills }) => {
    console.log(leftMills);
});
timer.start();
timer.pause();
timer.resume();
timer.stop();

相比其他的计时器,CountdownTimer 增加了 done 事件, 表示倒计时结束。 同时,在 tick 和 done 事件中,都增加了 leftMills 属性,表示剩余的时间。

ChangeLog

0.1.0

  1. 增加 EmitterTimer;
  2. 增加 on/emit 接口;