Skip to content

Function: debounce() ​

ts
function debounce<Args>(fn, wait): Debounced<Args>;

Delay fn until wait ms have passed without another call. Only the arguments of the last call are used.

Use it to throttle expensive work such as saving from onChange:

Type Parameters ​

Type Parameter
Args extends unknown[]

Parameters ​

ParameterType
fn(...args) => void
waitnumber

Returns ​

Debounced<Args>

Example ​

ts
const save = debounce((editor: Editor) => api.save(editor.getJSON()), 500)
createEditor({ onChange: save, ... })
window.addEventListener('beforeunload', () => save.flush())