Skip to content

debounce<T, V>

Official

A standard debounce export function. Use this to have a time-delayed export function only be called once in a given timeframe.

Import:

import { debounce } from 'obsidian';

Example:

const debounced = debounce((text: string) => {
console.log(text);
}, 1000, true);
debounced('foo'); // this will not be printed
await sleep(500);
debounced('bar'); // this will be printed to the console.

Signature:

function debounce(cb: (...args: T) => V, timeout: number | undefined, resetTimer: boolean | undefined): Debouncer<T, V>

Parameters:

ParameterTypeDescription
cb(…args: T) => VThe export function to call.
timeoutnumber | undefinedThe timeout to wait, in milliseconds.
resetTimerboolean | undefinedWhether to reset the timeout when the debounce export function is called again.

Returns: Debouncer<T, V> — a debounced export function that takes the same parameter as the original export function.

Example:

const debounced = debounce((text: string) => {
console.log(text);
}, 1000, true);
debounced('foo'); // this will not be printed
await sleep(500);
debounced('bar'); // this will be printed to the console.