export default {

  /**
   * @method setTimeout
   * @description behaves the same as setTimeout except uses requestAnimationFrame() where possible for better performance
   * @param {function} fn The callback function
   * @param {int} delay The delay in milliseconds
   */
  setTimeout: function(fn, delay) {
    if (!window.requestAnimationFrame) {
      return window.setTimeout(fn, delay);
    }

    let start = new Date().getTime();
    let handle = {};

    function loop() {
      let current = new Date().getTime();
      let delta = current - start;

      delta >= delay ? fn.call() : handle.value = window.requestAnimationFrame(loop);
    }

    handle.value = window.requestAnimationFrame(loop);
    return handle;
  },

  /**
   * @method clearTimeout
   * @description behaves the same as clearTimeout except uses cancelAnimationFrame() where possible for better performance
   * @param handle {int|object} callback function
   */
  clearTimeout: function(handle) {
    window.cancelAnimationFrame ? window.cancelAnimationFrame(handle.value) : clearTimeout(handle);
  }
};
