import { reactive, ref,onDeactivated,onActivated,onMounted,onUnmounted } from 'vue'

export function useInterval(callback,timeout=10000) {
  let timer
  function setTimer(){
    clearTimer()
      timer = setInterval(callback, timeout)
  }
  function clearTimer(){
    if(timer){
      clearInterval(timer)
      timer = null
    }
  }
  onMounted(setTimer)
  onActivated(setTimer)
  onDeactivated(clearTimer)
  onUnmounted(clearTimer)
  return {
    timer,
    clearTimer,
    setTimer
  }
}
