export default {

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

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

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

      if (delta >= delay) {
        fn.call();
        start = new Date().getTime();
      }

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

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

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