/**
 * 倒计时方法注册
 */
export function countdown(startFun, endFun, frequency, seconds = 1000) {
  var s = 0
  var bool = false
  var timeId = null

  function enjs() {
    if (timeId) {
      clearTimeout(timeId)
    }
    if (bool && endFun) {
      bool = false
      endFun()
    }
  }

  function djs() {
    timeId = setTimeout(function () {
      s--
      if (s <= 0) {
        enjs()
      } else {
        startFun && startFun(s)
        djs()
      }
    }, seconds)
  }
  return {
    start() {
      s = frequency
      bool = true
      startFun && startFun(s)
      djs()
    },
    end() {
      s = 0
      enjs()
    }
  }
}
