npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

vue-use-defer

v1.2.0

Published

A Vue 3 composable for deferring operations using requestAnimationFrame

Readme

useDefer

一个 Vue 3 组合式 API hook,用于使用 requestAnimationFrame 延迟执行操作。

安装

npm install vue-use-defer

使用方法

<template>
  <div>
    <p v-if="shouldShow">这个内容在第 30 帧后显示</p>
    <p v-if="shouldShowLater">这个内容在第 60 帧后显示</p>
  </div>
</template>

<script setup lang="ts">
import { useDefer } from "vue-use-defer";

const defer = useDefer(100); // 最多计数到 100 帧

// 检查是否应该显示内容
const shouldShow = defer(30);
const shouldShowLater = defer(60);
</script>

API

useDefer(maxCount?: number)

创建一个延迟函数。

参数

  • maxCount (可选): 最大帧数,默认为 100

返回值

返回一个函数,该函数接受一个数字参数 n,当当前帧数大于等于 n 时返回 true

工作原理

useDefer 使用 requestAnimationFrame 来计数动画帧。这对于以下场景特别有用:

  • 延迟加载非关键内容
  • 分阶段渲染复杂组件
  • 优化页面性能
  • 实现渐进式加载

示例场景

1. 延迟显示内容

<template>
  <div>
    <div v-if="showImmediate">立即显示</div>
    <div v-if="showAfter10Frames">10帧后显示</div>
    <div v-if="showAfter30Frames">30帧后显示</div>
  </div>
</template>

<script setup>
import { useDefer } from "vue-use-defer";

const defer = useDefer();
const showImmediate = true;
const showAfter10Frames = defer(10);
const showAfter30Frames = defer(30);
</script>

2. 渐进式加载列表

<template>
  <div>
    <div v-for="(item, index) in visibleItems" :key="item.id">
      {{ item.name }}
    </div>
  </div>
</template>

<script setup>
import { computed } from "vue";
import { useDefer } from "vue-use-defer";

const items = ref([
  /* 大量数据 */
]);
const defer = useDefer();

const visibleItems = computed(() => {
  const frame = defer(0)
    ? 0
    : defer(10)
    ? 10
    : defer(20)
    ? 20
    : items.value.length;
  return items.value.slice(0, frame);
});
</script>

兼容性

useDefer 具有良好的浏览器兼容性:

  • 现代浏览器: 使用原生的 requestAnimationFrame
  • 旧版浏览器: 自动降级到 setTimeout 模拟 60fps
  • Node.js 环境: 同样使用 setTimeout 降级方案

注意事项

  • 帧计数会在组件卸载时自动清理
  • 默认最大帧数为 100,可以根据需要调整
  • 在非浏览器环境中,使用 setTimeout 模拟动画帧,性能可能略有差异

许可证

MIT