@bnidev/js-utils
    Preparing search index...

    Class IntervalFn

    A utility class to repeatedly execute a function at a fixed interval with start/stop control.

    This class wraps the native setInterval and clearInterval APIs to provide an easy way to start and stop repeated execution of a function. The interval can optionally run a fixed number of times before automatically stopping.

    // ES Module
    import { IntervalFn } from '@bnidev/js-utils'

    // CommonJS
    const { IntervalFn } = require('@bnidev/js-utils')
    // Basic usage
    const interval = new IntervalFn(() => console.log('tick'), 1000)
    interval.start() // begins logging "tick" every second
    setTimeout(() => interval.stop(), 5000) // stops after 5 seconds

    // With max runs
    const interval = new IntervalFn(() => console.log('tick'), 1000, 3)
    interval.start() // will log "tick" 3 times then stop automatically
    Index

    Constructors

    Methods

    Constructors

    • Creates an IntervalFn instance.

      Parameters

      • fn: () => void

        The function to execute repeatedly.

      • ms: number

        Interval time in milliseconds.

      • OptionalmaxRuns: number

        Optional. The maximum number of times to run the function before stopping automatically.

      Returns IntervalFn

    Methods

    • Starts the interval timer. If already started, it does nothing.

      The function provided in the constructor will be called every ms milliseconds. If maxRuns was set, the interval will stop automatically after that many executions.

      Returns void

    • Stops the interval timer if it is running.

      Clears the internal timer ID so that the function no longer executes.

      Returns void