1.0.2 • Published 2 years ago

timers-classes v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

timers-classes

Usage

Timer classes for easy creation and use. It is possible to restart an already running timer, start the timer again after the end. It is possible to loop the start of timers and set the maximum number of repetitions.

Timer classes

All possible constructors params:

callback {Function} - callback function
time {number} - timer time
repeatCount {number=0} - number of additional repetitions
waitAsyncCallback {boolean=true} - wait async callback after start timer again
start {boolean=false} - start timer after creation

Timer example:

import { Timer } from 'timers-classes';
const timer = new Timer(() => {
  console.log('worked');
}, 100);
timer.start();
import { TimerBuilder } from 'timers-classes';

const timer = TimerBuilder.create(() => {
    console.log('worked');
  }, 100)
  .setStartImmediately(true)
  .build();

LoopTimer example:

import { LoopTimer } from 'timers-classes';

let workCount = 0;
const timer = new LoopTimer(() => {
  console.log('workCount', workCount++);
  if (workCount === 5) {
    timer.stop();
  }
}, 100);
timer.start();
import { TimerBuilder } from 'timers-classes';

let workCount = 0;
const timer = TimerBuilder.create(() => {
    console.log('workCount', workCount++);
  }, 100)
  .setStartImmediately(true)
  .buildLoop();
setTimeout(() => {
  timer.stop();
}, 1000);

RepeatingTimer example:

import { RepeatingTimer } from 'timers-classes';

let workCount = 0;
const timer = new RepeatingTimer(() => {
  workCount += 1;
  if (timer.remainingRepeatCount === 0) {
    console.log('result workCount', workCount);
  }
}, 100, 3);
timer.start();
import { TimerBuilder } from 'timers-classes';

let workCount = 0;
const timer = TimerBuilder.create(() => {
    console.log('workCount', workCount++);
    if (workCount === 3) {
      timer.stop();
    }
  }, 100)
  .setStartImmediately(true)
  .buildRepeating(5);