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

    Function everyNthCall

    • Creates a function that only invokes the provided callback every Nth time it's called.

      Parameters

      • fn: () => void

        The function to be conditionally executed.

      • n: number

        The number of calls between each execution. Must be ≥ 1.

      Returns () => void

      A new function that tracks call count and only invokes fn every Nth time.

      Will throw an error if n is less than 1 or not an integer.

      // ES Module
      import { everyNthCall } from 'your-library/timing';

      // CommonJS
      const { everyNthCall } = require('your-library/timing');
      const logEvery3 = everyNthCall(() => console.log("called"), 3);
      logEvery3() // nothing
      logEvery3() // nothing
      logEvery3() // logs "called"