Skip to content

d4n5h/Repeat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Repeat

A better way to do intervals.

Pros

  1. Repeat won't wait to the end of the first interval to run the callback. It will run it immediately.
  2. You can access all the internals from inside the callback, so you can stop, change the duration or even the callback on the fly.

Install

$ npm install @danisl99/repeat

Example

const Repeat = require('@danisl99/repeat');

// Without options the it will run forever with an interval of one second
// You can stop the interval externally or internally
const exampleOne = new Repeat((self) => {
    console.log(Date.now());
    // self.stop();
})

setTimeout(() => {
    exampleOne.stop();
}, 3000);

// With options
let counterOne = 0;
const exampleTwo = new Repeat({
    duration: 10000, // or use date:(Date.now()+5000)
    interval:500,
},(self) => {
    self.endTime -= 1000; // You can change the duration internally while it runs
    if(counterOne == 2){
        // Change callback on the fly
        self.callback = ()=>{
            console.log(Date.now());
        }
    }
    console.log(counterOne);
    counterOne++;
})

About

A better way to do intervals

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published