Design & Download
Your JS Library
in Seconds!!!

Snippet: doEvery()

Written by Christopher West (cwest) on November 29, 2015.
Call a function repeatedly, waiting a specific amount of time between calls. This is a different from setInterval() because the initial call is made immediately.
function doEvery(fn, interval, opt_arrParams) {
  function wrappedFunction() {
    end && fn.apply(end, opt_arrParams.concat([counter, start]));
  function end() {
    end = undefined;
  opt_arrParams = opt_arrParams || [];
  setTimeout(wrappedFunction, 0);
  var id = setInterval(wrappedFunction, interval), start = new Date, counter = 0;
  return end;

doEvery(...) API Documentation


Calls a function repeatedly waiting the specified amount of time between each call. Similar to setInterval() but instead of waiting on the initial call, the initial is made immediately.


  1. fn {Function(..., timesCalled, start)}:
    The function to be called repeatedly after waiting specified interval of time between each call. The this object will be a function that when execute will prevent future calls from being made. The second-to-last parameter passed to this function when called will indicate how many times the function was called. The last parameter passed will indicate when doEvery(...) was called.
  2. interval {number}:
    A positive integer indicating how many milliseconds to wait in between calls to fn
  3. opt_arrParams {Array}:
    Optional. Defaults to the empty array. An array of the arguments to be passed to fn(). In addition, the last two parameters passed will be the amount of times the wrapper function called fn() and the starting time.


Returns a function that when ends the repeated calling of fn() by the created wrapper function.