index.ts 665 B

12345678910111213141516171819202122232425262728293031
  1. import { onMounted, ref, watch } from 'vue'
  2. import type { Ref } from 'vue'
  3. export const useThrottleRender = (loading: Ref<boolean>, throttle = 0) => {
  4. if (throttle === 0) return loading
  5. const throttled = ref(false)
  6. let timeoutHandle = 0
  7. const dispatchThrottling = () => {
  8. if (timeoutHandle) {
  9. clearTimeout(timeoutHandle)
  10. }
  11. timeoutHandle = window.setTimeout(() => {
  12. throttled.value = loading.value
  13. }, throttle)
  14. }
  15. onMounted(dispatchThrottling)
  16. watch(
  17. () => loading.value,
  18. (val) => {
  19. if (val) {
  20. dispatchThrottling()
  21. } else {
  22. throttled.value = val
  23. }
  24. }
  25. )
  26. return throttled
  27. }