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 🙏

© 2026 – Pkg Stats / Ryan Hefner

loop-timer

v1.0.6

Published

模拟 linux Crontab 的 js 定时任务,可以指定时间来执行任务,由于 JS 是单线程,所以如果指定的任务是个大计算量的任务,有可能会导致其他任务被延时执行,如果对时间准确性要求很高的,请慎用. 项目里面同时只会存在一个计时器, 如果有没有可以执行事件的时候,计时器将会被销毁,有新的可执行事件才会再次开启新的计时器.

Downloads

2

Readme

loop-timer

模拟 linux Crontab 的 js 定时任务,可以指定时间来执行任务,由于 JS 是单线程,所以如果指定的任务是个大计算量的任务,有可能会导致其他任务被延时执行,如果对时间准确性要求很高的,请慎用. 项目里面同时只会存在一个计时器, 如果有没有可以执行事件的时候,计时器将会被销毁,有新的可执行事件才会再次开启新的计时器.

使用方法

  1. 使用了单例设计,获取实例对象

    let timer = LoopTimer.getInstance();
  2. 通过这个实例对象,则可以将事件注册到定时器里,根据使用者指定的时间来调用事件.

    //nodejs中使用
    const loopTimer = require('loop-timer').default;
    function log(){
        console.log('timer run:', new Date().getTime());
    }
    let symbol: Symbol = loopTimer.registry(log,  {
        seconds: '*/10',
        minutes: '*',
        hour: '*'
    }, true, (code, data) => {
        console.log(code, data);
    });
    //取消刚刚注册的事件
    loopTimer.unRegister(symbol);
    //---------------------------------------------------------
    //vuejs中使用
    mounted(){
        let timer = LoopTimer.getInstance();
        let symbol = timer.registry((){
            console.log('timer run:', new Date().getTime());
        }, {
            seconds: '10',
            minutes: '*',
            hour: '*'
        }, false, (code, data) => {
            console.log(code, data);
        });
        //取消刚刚注册的事件
        timer.unRegister(symbol);
    },
    
    //代码解释
    // nodejs那份代码意思是 隔10秒执行一次任务,
    // vuejs的示例中代表, 每次的第10秒的时候,执行任务.
    // 是否轮询一直执行任务呢, 就看第二个参数是true还是false,如果为true 一直执行,如果为false则只执行一次
    // 新增了结果回调, 执行的方法如果有返回值,则会将返回值通过匿名函数的方式传回给调用者
    // 支持了匿名函数的调用,和取消匿名函数的执行, 通过反注册来取消. 注册事件的时候会返回一个Symbol值, 通过该值可以取消注册的事件, 让事件不再被执行.

传参的参数说明

    /**
    * 注册轮询事件, 注册以后返回一个Symbol, 可以通过该Symbol参数来取消注册的事件
    * @param func 要执行的方法, 支持普通函数,匿名函数,异步函数
    * @param frequency 执行的频率
    * @param isLoop 是否轮询,不停执行, 默认是false,只执行一次
    * @param callback 回调结果 请根据回调结果的code先判断下是否返回值符合预期
    */
    registry<T>(func: Function, frequency?: iTiming, isLoop?: boolean, callback?: {
    (code: eResultCode, data?: T): Symbol;

eResultCode 的接口定义

    export enum eResultCode {
        /**
        * 任务执行成功,
        */
        SUCCESS = 0,
        /**
        * 任务执行完毕,但是结果不尽如人意(async函数才可能会有该警告提示, 就是结果走到了Promise的then函数的err回调)
        */
        WRANING = 1,
        /**
        * 任务执行发生错误
        */
        ERROR = 2
    }

iTiming 传值参考了 linux Crontab 的定时任务 '* * * * *'->分、时、日、月、周五种, 暂时只支持到了小时,如果有前端朋友不懂 linux 的 Crontab 的参数含义,请看下面的接口说明和示例, 或者自行查询 linux 的 Crontab

    export interface iTiming {
        /**
        * 秒级,
        * 传值示例->
        * '*': 每秒,
        * '10':第10秒,
        * '* /2': 每隔2秒 注意,星号与斜杠间无空格
        */
        seconds: string;
        /**
        * 分钟级,
        * 传值示例->
        * '*': 每分钟,
        * '10':第10分钟,
        * '* /2': 每隔2分钟 注意,星号与斜杠间无空格
        */
        minutes: string;
        /**
        * 小时级,
        * 传值示例->
        * '*': 每小时,
        * '10':第10小时,
        * '* /2': 每隔2小时 注意,星号与斜杠间无空格
        */
        hour: string;
    }

注:

1. 暂时只支持了秒,分,小时, 后续会陆续完善添加天,周,月的支持.

2. 参考 Crontab, 继续完善参数类型的支持,比如 3,15 8-11/1 * * 1 .