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

nice-timer

v1.3.2

Published

Timer

Downloads

26

Readme

安装

npm i nice-timer -S

快速上手

import timer, { Timer, TimerInstance, wait } from 'nice-timer';

// 循环
const handle = timer({
  time: 1000,
  loop: true,
  cb(TimerOption) {
    app.innerText = Date();
  }
});
// 主动关闭循环
handle();
// 循环简写
const handle = timer(
  () => {
    app.innerText = Date();
  },
  1000,
  true
);

// 不循环
const handle2 = timer({
  time: 2000,
  loop: false,
  cb(TimerOption) {
    app.innerText = Date();
  }
});
// 不循环的简写
const handle2 = timer(() => {
  app.innerText = Date();
}, 2000);

// 循环2次结束
const handle3 = timer({
  time: 1000,
  loop: 2,
  cb(TimerOption) {
    app.innerText = Date();
  }
});
// 循环2次简写
const handle3 = timer(
  () => {
    app.innerText = Date();
  },
  1000,
  2
);

// 根据 check 的返回状态执行定时任务(简写不支持check和timerKey)
let a = 0;
const handle4 = timer({
  time: 1000,
  loop: true,
  check(TimerOption) {
    a++;
    // 仅在a===5或者a===10的时候运行定时任务
    if (a === 5 || a === 10) {
      return true;
    }
    return false;
  },
  cb(TimerOption) {
    app.innerText = Date();
  }
});

// 更新定时任务
const id = Symbol();
const handle = timer({
  time: 1000,
  cb() {
    // 每隔1秒打印日志1
    console.log(1);
  },
  timerKey: id
});
// 过5秒后更新标识符为id的这个定时任务,让他每隔3秒打印一下日志2
timer(() => {
  timer({
    time: 3000,
    cb() {
      console.log(2);
    },
    timerKey: id
  });
}, 5000);
// 自动关闭
handle();

// 直接使用Timer类创建新的实例
const TimerInstance2 = new Timer();
const handle5 = TimerInstance2.create({
  time: 1000,
  loop: true,
  cb(TimerOption) {
    app.innerText = Date();
  }
});
// 简写
TimerInstance2.create(() => {
  app.innerText = Date();
});
// 3秒后暂停 TimerInstance2 实例内所有定时任务
setTimeout(() => {
  TimerInstance2.stop();
}, 3000);
// 6秒后重新开启 TimerInstance2 实例内所有定时任务
setTimeout(() => {
  TimerInstance2.run();
}, 6000);

// 等待3秒
await wait(3000);
// 或者
wait(3000).then(() => {});

脚本引入

<script src="nice-timer.min.js"></script>
<script>
    const app = document.getElementById('app');
    const { timer, Timer, TimerInstance } = niceTimer;
    const handle = timer({
      time: 1000,
      loop: true,
      cb(TimerOption) {
        app.innerText = Date();
      }
    });
    // 主动关闭 handle 定时任务
    handle()
    // 创建新的实例
    const TimerInstance2 = new Timer()
    const a2 = TimerInstance2.create({
      time: 1000,
      loop: true,
      cb(TimerOption) {
        app.innerText = Date();
      }
    })
    // 停止 TimerInstance2 实例里面的所有定时任务
    TimerInstance2.stop()
    // 运行 TimerInstance2 实例里面的所有定时任务
    TimerInstance2.run()
  </script>

nice-timer

import timer, { Timer, TimerInstance } from 'nice-timer';
import { TimerOption, CreateOption, Handler, KeyType } from 'nice-timer/types';

export interface CreateOption {
  // 轮询时间
  time: number;
  // 定时后的回调函数
  cb: Handler;
  // 循环
  loop?: boolean | number;
  // 检查函数,返回true则表示该任务运行,否则不允许该cb,返回false时不运行cb但是该total还是会继续叠加
  check?(params: TimerOption): boolean;
  // 定时任务的标识符(建议使用symbol),相同的 timerKey 将进行替换传递进来的其他参数,定时任务将按照新的参数进行运行
  timerKey?: KeyType;
}

export interface TimerOption extends CreateOption {
  // 该定时任务的唯一值
  id: symbol;
  // 该任务的最后一次运行时间
  lastTime: number;
  // 该任务总运行次数
  total: number;
  // 该任务cb运行次数
  times: number;
  // 关闭当前任务函数
  targetCloseHandler: () => void;
}

const option: CreateOption = {
  time: 1000,
  cb(TimerOption) {
    console.log(new Date());
  }
};
const handle = timer(option);

// 简写
const handle2 = timer(() => {
  console.log(new Date());
}, 1000);
const TimerInstance2 = new Timer();
const handle2 = TimerInstance2.create(option);