Function secondsMsSource

Returns function which produces a random number of seconds, with a unit of milliseconds. Maximum value is exclusive. Use secondsMs to return a random value directly, or secondsMsSource to return a function.

// Create function
const f = secondsMsSource(5000);
// Produce a value
const value = f();
// Random milliseconds between 1000-4999
const value = secondsMsSource({ max:5, min:1 })();
// Note the extra () at the end to execute the function

It's a very minor function, but can make code a little more literate:

// Random timeout of up to 5 seconds
setTimeout(() => { ...}, secondsMsSource(5));
  • Parameters

    • maxSecondsOrOptions: number | Readonly<{
          max: number;
          min?: number;
          source?: RandomSource;
      }>

      Maximum seconds, or options.

    Returns RandomSource

    Milliseconds