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

@just4/polling

v0.1.0-beta.1

Published

Just for making polling task easier to create.

Downloads

4

Readme

@just4/polling

执行轮询定时)任务。与纯粹的 setTimeout 或 setInterval 实现的定时任务相比,本模块还支持在异步操作(比如对后端接口的请求)完成之后,再进行下一轮计时。

安装

npm i @just4/polling

调用

基本调用

const polling = new Polling(() => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('executed');
      resolve();
    }, 1000);
  });
}, {
  // 2 秒执行一次
  interval: 2000
});
// 调用 start 后会马上执行一次操作函数
polling.start();

出错后停止轮询

可以通过传入 breakOnError: true 在执行出错的时候停止轮询:

const polling = new Polling(() => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (Math.random < 0.5) {
        resolve();
      } else {
        reject();
      }
    }, 1000);
  });
}, {
  interval: 2000,
  breakOnError: true
});
polling.start();

更新轮询选项

可以调用实例方法 updateOptions 更新轮询选项:

const polling = new Polling(() => {
  return new Promise((resolve) => {
    setTimeout(() => {
      polling.updateOptions({
        interval: Math.random() < 0.5 ? 5000 : 2000
      });
      resolve();
    }, 1000);
  });
}, {
  interval: 2000,
  breakOnError: true
});
polling.start();

轮询过程中立刻执行一次操作函数

const polling = new Polling(() => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('executed');
      resolve();
    }, 1000);
  });
}, {
  interval: 2000,
  breakOnError: true
});
polling.start();

setTimeout(() => {
  // 注意,如果操作函数是异步的而且当前还未执行完,
  // 那么 execImmediately 会等待它执行完之后才再次执行操作函数
  polling.execImmediately();
}, 3000);

相关文档