@danisl99/repeat
Version:
A better way to do intervals
29 lines (26 loc) • 771 B
JavaScript
const Repeat = require('./index');
// 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++;
})