Skip to content

DoOrDelay Utility function #3373

@jmarren

Description

@jmarren

There are several occurrences of this particular functionality, so I'd like to propose adding the following utility function.

If you do a search for getWindow().setTimeout you will see that this function is appropriate for the majority of cases that window.setTimeout is used.

It is fairly self-explanatory:

/**
   * @param {number} delay
   * @param {function} callback
   * @param {function} elseCallback (optional)
   * @returns { any }
   */
  function doOrDelay(callback, delay, elseCallback) {
    if (delay && delay > 0) {
      return getWindow().setTimeout(callback, delay);
    } else if (elseCallback) {
      return elseCallback();
    } else {
      return callback();
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions