Giter VIP home page Giter VIP logo

smartinterval's Introduction

SmartInterval

An interval that can be started/stopped and evaluates sequentially

What I wanted to achieve

SmartInterval creates an interval that has the following features:

  • Can be paused/stopped
  • Can be forced to execute
  • Evaluates sequentially (no other call is made before the current call is evaluated)

Install

npm i smartinterval

Getting started

This library is particularly useful because it evaluates async code periodically and sequentially. Another call of your async job won't be sent until the previous call has been evaluated. If you force it you will get an additional immediate call.

let SmartInterval = require("smartinterval");

// Initialize the interval by new-ing the SmartInterval constructor
// It accepts 2 arguments:
// - An async function to be executed (job)
// - The time to wait between cycles (delay)
let dataFetcher = new SmartInterval(
    async () => {
        await getSomeData();
        await getSomeOtherData();
    },
    3000
);

// You can start it
dataFetcher.start();

// You can stop it
dataFetcher.stop();

// You can force it to run a cycle
// This is particularly useful when you receive an interaction and you want to immediately fetch the data
dataFetcher.forceExecution();

smartinterval's People

Contributors

4skinskywalker avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.