
import { ref, onMounted, onUnmounted, watch } from 'vue'

export const useTimeOut = (func, depend = [], once = true) => {
  const timeOutSelf = ref(null)

  const invoker = () => {
    
    if (once) {
      func()
    } else {
      func()
      timeOutSelf.value = setTimeout(() => {
        invoker()
      }, 1000 * 60 * 5)
    }
  }

  onMounted(() => {
    if (depend.length) {
      depend.forEach(item => {
        watch(() => item.value, () => {
          invoker()
        }, { deep: true, immediate: true })
      })
    } else {
      invoker()
    }
  })
  onUnmounted(() => {
    clearTimeout(timeOutSelf.value)
  })
}