Skip to content

micro-js/debounce

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

debounce

Build status Git tag NPM version Code style

Debounce a function

Installation

$ npm install @f/debounce

Usage

Debounce takes a function and a time in milliseconds and returns a function that, once called, will not run until time milliseconds have elapsed and will drop all subsequent calls in the interim period. This is useful if you have an event that might happen many times in a short period but that you only want to respond to once after they have all stopped, e.g.

var debounce = require('@f/debounce')
window.addEventListener('resize', debounce(relayout))

API

debounce(fn, time)

  • fn - The function you want to debounce
  • time - The time in milliseconds. Defaults to 0.

Returns: A debounced version of fn. Each time fn is called, it will return a cancel function that you can use to cancel the invocation of fn.

License

MIT