export default function () {
  /**
   * 触发周期性事件
   */
  let count = 0;
  window.dispatchEvent(new CustomEvent("timer", { bubbles: false, detail: { count: count } }));
  window.setInterval(() => {
    count++;
    if (count > 36000000) {
      count = 0;
    }
    window.dispatchEvent(new CustomEvent("timer", { bubbles: false, detail: { count: count } }));
  }, 1000);

}


const map = {}

/**
 * 每seconds秒执行一次fn
 * @param {Number} seconds 
 * @param {Function} fn 
 */
export const timerBind = (seconds, fn) => {
  map[fn] = (event) => {
    //每seconds秒执行一次
    if (event.detail.count % seconds != 0) {
      return
    }
    fn()
  }
  window.addEventListener("timer", map[fn])
}

/**
 * 解除函数的绑定
 * @param {Function} fn 
 */
export const timerUnBind = (fn) => {
  window.removeEventListener("timer", map[fn])
  delete map[fn]
}