🚀 Socket Launch Week 🚀 Day 1: Introducing .NET Support in Socket.Learn More
Socket
Sign inDemoInstall
Socket

setinterval

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

setinterval

[![NPM version][npm-image]][npm-url] [![npm download][download-image]][download-url]

0.2.4
latest
Source
npm
Version published
Weekly downloads
502
-46.08%
Maintainers
1
Weekly downloads
 
Created
Source

setinterval

NPM version npm download

We all know the drawbacks of the built-in setInterval in Node.js(actually js itself).

It's more reasonable to start measuring period after every async task gets done. So here it is.

Install

$ npm i setinterval

Example

  const Timer = require('setinterval');
  const t = new Timer(async () => {
    const user = await db.User.get(id);
    console.log(user);
  }, 1000);

  // start timer
  t.setInterval();

  // after some time...

  // clear timer
  t.clearInterval();

API

new Timer(fn, period)

Timer constructor.

Params:

  • fn(required): function executed after every period. Should be a Promise or async function or generator function or thunk.
  • period(required): timer period(units: milliseconds).

setInterval([initialDelay], [invokeImmediate])

Start timer after a certain delay(defaults to 0) and can decide if invoke immediately(defaults to false).

Params:

  • initialDelay(optional): Delay period(units: milliseconds) before timer gets triggered. default: 0
  • invokeImmediate(optional): specify if the timer function invoke immediately.default: false

clearInterval()

Stop timer(can be restart again).

Events

tick

Triggered each time fn is finished, whenever a error is thrown. You can cancel the timer in this event. A count parameter is passed in the event handler which stands for how many times fn has been called.

timer.on('tick', count => {
  timer.clearInterval();
});

error

Triggered when error thrown from fn.

timer.on('error', e => {
  logger.info(e.stack);
});

License

MIT

Keywords

setInterval

FAQs

Package last updated on 26 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts