userLazyLoadComponents.ts 520 B

1234567891011121314151617181920212223
  1. import { ref, onUnmounted } from 'vue';
  2. function useLazyLoadComponents(maxCount = 100) {
  3. const frameCount = ref(0);
  4. let rafId: number;
  5. function updateFrameCount() {
  6. rafId = requestAnimationFrame(() => {
  7. frameCount.value++
  8. if (frameCount.value >= maxCount) return
  9. updateFrameCount()
  10. })
  11. }
  12. updateFrameCount()
  13. onUnmounted(() => {
  14. cancelAnimationFrame(rafId)
  15. });
  16. return function defer (n: number) {
  17. return frameCount.value >= n
  18. }
  19. }
  20. export default useLazyLoadComponents