kliping.pro
  • Docs
  • Explore
  • Builder
  • Directory
  • Colors
  • Typeset
Saved
Hooks
  • Getting Started2
  • State7
  • useBoolean
  • useControllableValue
  • useCounter
  • useDebounce
  • useResetState
  • useThrottle
  • useToggle
  • Advanced6
  • Lifecycle16
  • Browser21
  • Development1
  • Third-Party4
  • Libraries4
  1. Hooks
  2. State
  3. useThrottle

useThrottle

PreviousNext

Salinan sebuah nilai yang diperbarui paling sering sekali per jeda.

Pasangan useDebounce, untuk kasus sebaliknya: nilai yang berubah terus menerus dan tidak akan pernah "berhenti" — posisi pointer, jarak scroll, ukuran jendela. Debounce di sana tidak pernah memicu apa pun sampai gerakannya berhenti; throttle tetap memberi kabar sepanjang jalan.

Installation ​

pnpm dlx kliping@latest add use-throttle

Ikut terpasang sendiri: use-throttle-fn.

Salin dulu use-throttle-fn — masing-masing memuat sumbernya di halamannya sendiri — lalu tempel berkas ini.

ts
import { getCurrentScope, onScopeDispose, ref, toValue, watch } from 'vue'
import type { MaybeRefOrGetter, Ref } from 'vue'
import { useThrottleFn } from '@/composables/use-throttle-fn'
import type { UseThrottleFnOptions } from '@/composables/use-throttle-fn'

export function useThrottle<T>(
  value: MaybeRefOrGetter<T>,
  wait: MaybeRefOrGetter<number> = 1000,
  options?: UseThrottleFnOptions,
) {
  const throttled = ref(toValue(value)) as Ref<T>

  const { run, cancel } = useThrottleFn(
    (next: T) => {
      throttled.value = next
    },
    wait,
    options,
  )

  watch(
    () => toValue(value),
    (next) => run(next),
  )

  if (getCurrentScope()) onScopeDispose(cancel)

  return throttled
}

Requirements ​

RequirementValue
ProjectVue 3.5+, alias @ — disiapkan kliping init
Kliping itemsuse-throttle-fn
Filescomposables/use-throttle.ts

API Reference ​

ts
export function useThrottle<T>(
  value: MaybeRefOrGetter<T>,
  wait: MaybeRefOrGetter<number> = 1000,
  options?: UseThrottleFnOptions,
)

Parameters ​

ParameterTypeDefaultDescription
valueMaybeRefOrGetter<T>—Value to follow. May be a ref or getter.
wait?MaybeRefOrGetter<number>1000Minimum milliseconds between updates. May be reactive.
options?UseThrottleFnOptions—Leading and trailing edge settings.
options.leading?boolean—Invoke the first call immediately. Default: true.
options.trailing?boolean—Invoke the last call after the wait elapses. Default: true.

Returns ​

Ref<T, T> — Copy of value updated at most once per wait. Reactive.

Credits ​

Berasal dari shadcn-hooks (MIT, Copyright (c) 2025 Brendan Dash). Bentuk API-nya dari ahooks lewat shadcn-hooks. Rantai lengkapnya di Credits.

PrevioususeResetStateNext useToggle

On This Page

InstallationRequirementsAPI ReferenceParametersReturnsCredits

© 2026 Kliping. Free components are open source under the MIT License.

LicenseCreditsRoadmap