| 1 |
# throttle-debounce |
| 2 |
|
| 3 |
[![Build Status][ci-img]][ci] |
| 4 |
[![BrowserStack Status][browserstack-img]][browserstack] |
| 5 |
[![Mentioned in Awesome Micro npm Packages][awesome-img]][awesome] |
| 6 |
|
| 7 |
Throttle and debounce functions. |
| 8 |
|
| 9 |
This module is the same as [jquery-throttle-debounce][jquery-throttle-debounce] |
| 10 |
([with some differences](#differences-with-original-module)), but it’s |
| 11 |
transferred to ES Modules and CommonJS format. |
| 12 |
|
| 13 |
## Install |
| 14 |
|
| 15 |
```sh |
| 16 |
npm install throttle-debounce --save |
| 17 |
``` |
| 18 |
|
| 19 |
## Usage |
| 20 |
|
| 21 |
### `throttle` |
| 22 |
|
| 23 |
```js |
| 24 |
import { throttle } from 'throttle-debounce'; |
| 25 |
|
| 26 |
const throttleFunc = throttle( |
| 27 |
1000, |
| 28 |
(num) => { |
| 29 |
console.log('num:', num); |
| 30 |
}, |
| 31 |
{ noLeading: false, noTrailing: false } |
| 32 |
); |
| 33 |
|
| 34 |
// Can also be used like this, because noLeading and noTrailing are false by default |
| 35 |
const throttleFunc = throttle(1000, (num) => { |
| 36 |
console.log('num:', num); |
| 37 |
}); |
| 38 |
|
| 39 |
throttleFunc(1); // Will execute the callback |
| 40 |
throttleFunc(2); // Won’t execute callback |
| 41 |
throttleFunc(3); // Won’t execute callback |
| 42 |
|
| 43 |
// Will execute the callback, because noTrailing is false, |
| 44 |
// but if we set noTrailing to true, this callback won’t be executed |
| 45 |
throttleFunc(4); |
| 46 |
|
| 47 |
setTimeout(() => { |
| 48 |
throttleFunc(10); // Will execute the callback |
| 49 |
}, 1200); |
| 50 |
|
| 51 |
// Output |
| 52 |
// num: 1 |
| 53 |
// num: 4 |
| 54 |
// num: 10 |
| 55 |
``` |
| 56 |
|
| 57 |
### `debounce` |
| 58 |
|
| 59 |
```js |
| 60 |
import { debounce } from 'throttle-debounce'; |
| 61 |
|
| 62 |
const debounceFunc = debounce( |
| 63 |
1000, |
| 64 |
(num) => { |
| 65 |
console.log('num:', num); |
| 66 |
}, |
| 67 |
{ atBegin: false } |
| 68 |
); |
| 69 |
|
| 70 |
// Can also be used like this, because atBegin is false by default |
| 71 |
const debounceFunc = debounce(1000, (num) => { |
| 72 |
console.log('num:', num); |
| 73 |
}); |
| 74 |
|
| 75 |
// Won’t execute the callback, because atBegin is false, |
| 76 |
// but if we set atBegin to true, this callback will be executed. |
| 77 |
debounceFunc(1); |
| 78 |
|
| 79 |
debounceFunc(2); // Won’t execute callback |
| 80 |
debounceFunc(3); // Won’t execute callback |
| 81 |
|
| 82 |
// Will execute the callback, |
| 83 |
// but if we set atBegin to true, this callback won’t be executed. |
| 84 |
debounceFunc(4); |
| 85 |
|
| 86 |
setTimeout(() => { |
| 87 |
debounceFunc(10); // Will execute the callback |
| 88 |
}, 1200); |
| 89 |
|
| 90 |
// Output |
| 91 |
// num: 4 |
| 92 |
// num: 10 |
| 93 |
``` |
| 94 |
|
| 95 |
### Cancelling |
| 96 |
|
| 97 |
Debounce and throttle can both be cancelled by calling the `cancel` function. |
| 98 |
|
| 99 |
```js |
| 100 |
const throttleFunc = throttle(300, () => { |
| 101 |
// Throttled function |
| 102 |
}); |
| 103 |
|
| 104 |
throttleFunc.cancel(); |
| 105 |
|
| 106 |
const debounceFunc = debounce(300, () => { |
| 107 |
// Debounced function |
| 108 |
}); |
| 109 |
|
| 110 |
debounceFunc.cancel(); |
| 111 |
``` |
| 112 |
|
| 113 |
The logic that is being throttled or debounced will no longer be called. |
| 114 |
|
| 115 |
To cancel only one upcoming debounced call, you can pass `upcomingOnly: true` |
| 116 |
option to `cancel` function: |
| 117 |
|
| 118 |
```js |
| 119 |
const debounceFunc = debounce(300, () => { |
| 120 |
// Debounced function |
| 121 |
}); |
| 122 |
|
| 123 |
debounceFunc(); // will not be invoked |
| 124 |
|
| 125 |
debounceFunc.cancel({ upcomingOnly: true }); |
| 126 |
|
| 127 |
debounceFunc(); // will be invoked |
| 128 |
``` |
| 129 |
|
| 130 |
## API |
| 131 |
|
| 132 |
### throttle(delay, callback, { noLeading, noTrailing, debounceMode }) |
| 133 |
|
| 134 |
Returns: `Function` |
| 135 |
|
| 136 |
Throttle execution of a function. Especially useful for rate limiting execution |
| 137 |
of handlers on events like resize and scroll. |
| 138 |
|
| 139 |
#### delay |
| 140 |
|
| 141 |
Type: `Number` |
| 142 |
|
| 143 |
A zero-or-greater delay in milliseconds. For event callbacks, values around 100 |
| 144 |
or 250 (or even higher) are most useful. |
| 145 |
|
| 146 |
#### callback |
| 147 |
|
| 148 |
Type: `Function` |
| 149 |
|
| 150 |
A function to be executed after delay milliseconds. The `this` context and all |
| 151 |
arguments are passed through, as-is, to `callback` when the throttled-function |
| 152 |
is executed. |
| 153 |
|
| 154 |
#### noTrailing |
| 155 |
|
| 156 |
Type: `Boolean` |
| 157 |
|
| 158 |
Optional, defaults to false. If noTrailing is true, callback will only execute |
| 159 |
every `delay` milliseconds while the throttled-function is being called. If |
| 160 |
noTrailing is false or unspecified, callback will be executed one final time |
| 161 |
after the last throttled-function call. (After the throttled-function has not |
| 162 |
been called for `delay` milliseconds, the internal counter is reset) |
| 163 |
|
| 164 |
#### noLeading |
| 165 |
|
| 166 |
Type: `Boolean` |
| 167 |
|
| 168 |
Optional, defaults to false. If noLeading is false, the first throttled-function |
| 169 |
call will execute callback immediately. If noLeading is true, the first the |
| 170 |
callback execution will be skipped. It should be noted that callback will never |
| 171 |
executed if both noLeading = true and noTrailing = true. |
| 172 |
|
| 173 |
#### debounceMode |
| 174 |
|
| 175 |
Type: `Boolean` |
| 176 |
|
| 177 |
If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` |
| 178 |
ms. If `debounceMode` is false (at end), schedule `callback` to execute after |
| 179 |
`delay` ms. |
| 180 |
|
| 181 |
### debounce(delay, callback, { atBegin }) |
| 182 |
|
| 183 |
Returns: `Function` |
| 184 |
|
| 185 |
Debounce execution of a function. Debouncing, unlike throttling, guarantees that |
| 186 |
a function is only executed a single time, either at the very beginning of a |
| 187 |
series of calls, or at the very end. |
| 188 |
|
| 189 |
#### delay |
| 190 |
|
| 191 |
Type: `Number` |
| 192 |
|
| 193 |
A zero-or-greater delay in milliseconds. For event callbacks, values around 100 |
| 194 |
or 250 (or even higher) are most useful. |
| 195 |
|
| 196 |
#### callback |
| 197 |
|
| 198 |
Type: `Function` |
| 199 |
|
| 200 |
A function to be executed after delay milliseconds. The `this` context and all |
| 201 |
arguments are passed through, as-is, to `callback` when the debounced-function |
| 202 |
is executed. |
| 203 |
|
| 204 |
#### atBegin |
| 205 |
|
| 206 |
Type: `Boolean` |
| 207 |
|
| 208 |
Optional, defaults to false. If `atBegin` is false or unspecified, callback will |
| 209 |
only be executed `delay` milliseconds after the last debounced-function call. If |
| 210 |
`atBegin` is true, callback will be executed only at the first |
| 211 |
debounced-function call. (After the throttled-function has not been called for |
| 212 |
`delay` milliseconds, the internal counter is reset). |
| 213 |
|
| 214 |
## Differences with original module |
| 215 |
|
| 216 |
- Dependancy on jQuery is removed, so if you rely on GUIDs set by jQuery, plan |
| 217 |
accordingly |
| 218 |
- There is no standalone version available, so don’t rely on `$.throttle` and |
| 219 |
`$.debounce` to be available |
| 220 |
|
| 221 |
## Browser support |
| 222 |
|
| 223 |
Tested in Chrome 72, Edge 15, Firefox 65 and should work in all modern browsers |
| 224 |
([support based on Browserslist configuration](https://browserslist.dev/?q=bGFzdCAzIG1ham9yIHZlcnNpb25zLCBzaW5jZSAyMDE5LCBlZGdlID49IDE1LCBub3QgaWUgPiAw)). |
| 225 |
|
| 226 |
## Test |
| 227 |
|
| 228 |
For automated tests, run `npm run test:automated` (append `:watch` for watcher |
| 229 |
support). |
| 230 |
|
| 231 |
## License |
| 232 |
|
| 233 |
<!-- prettier-ignore-start --> |
| 234 |
|
| 235 |
MIT © [Ivan Nikolić](http://ivannikolic.com) |
| 236 |
|
| 237 |
[ci]: https://github.com/niksy/throttle-debounce/actions?query=workflow%3ACI |
| 238 |
[ci-img]: https://github.com/niksy/throttle-debounce/actions/workflows/ci.yml/badge.svg?branch=master |
| 239 |
[browserstack]: https://www.browserstack.com/ |
| 240 |
[browserstack-img]: https://img.shields.io/badge/browser%20testing-BrowserStack-informational?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2NCA2NCI+CiAgPGRlZnMvPgogIDxyYWRpYWxHcmFkaWVudCBpZD0iYSIgY3g9IjIwLjk0Mjk3NiIgY3k9IjI4LjA5NDY3ODczIiByPSIzLjc5MTM0MTQxIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSI+CiAgICA8c3RvcCBvZmZzZXQ9IjAiIHN0b3AtY29sb3I9IiM3OTc5NzkiLz4KICAgIDxzdG9wIG9mZnNldD0iMSIgc3RvcC1jb2xvcj0iIzRjNGM0YyIvPgogIDwvcmFkaWFsR3JhZGllbnQ+CiAgPGcgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoLTI5LjcyOTIwNCAtNTcuMTg3NjExKSBzY2FsZSgyLjk3MjkyKSI+CiAgICA8Y2lyY2xlIGN4PSIyMC43ODkiIGN5PSIzMC4wMjUiIHI9IjEwLjczOSIgZmlsbD0iI2Y0Yjk2MCIvPgogICAgPGNpcmNsZSBjeD0iMTkuNyIgY3k9IjI4LjkzNiIgcj0iOS43IiBmaWxsPSIjZTY2ZjMyIi8+CiAgICA8Y2lyY2xlIGN4PSIyMS4wMzYiIGN5PSIyNy42OTkiIHI9IjguNDEzIiBmaWxsPSIjZTQzYzQxIi8+CiAgICA8Y2lyY2xlIGN4PSIyMS42NzkiIGN5PSIyOC4zNDIiIHI9IjcuNzIiIGZpbGw9IiNiZGQwNDEiLz4KICAgIDxjaXJjbGUgY3g9IjIxLjEzNSIgY3k9IjI4LjkzNiIgcj0iNy4xNzYiIGZpbGw9IiM2ZGI1NGMiLz4KICAgIDxjaXJjbGUgY3g9IjE5Ljk5NyIgY3k9IjI3Ljc0OCIgcj0iNS45ODgiIGZpbGw9IiNhZWRhZTYiLz4KICAgIDxjaXJjbGUgY3g9IjIwLjkzNyIgY3k9IjI2Ljc1OCIgcj0iNS4wNDgiIGZpbGw9IiM1NmI4ZGUiLz4KICAgIDxjaXJjbGUgY3g9IjIxLjU4IiBjeT0iMjcuNDUxIiByPSI0LjQwNSIgZmlsbD0iIzAwYjFkNSIvPgogICAgPGNpcmNsZSBjeD0iMjAuOTM3IiBjeT0iMjguMDQ1IiByPSIzLjc2MSIgZmlsbD0idXJsKCNhKSIvPgogICAgPGNpcmNsZSBjeD0iMjAuOTM3IiBjeT0iMjguMDQ1IiByPSIzLjc2MSIgZmlsbD0iIzIyMWYxZiIvPgogICAgPGVsbGlwc2UgY3g9Ii0xNS4xNTkiIGN5PSIzMS40MDEiIGZpbGw9IiNmZmYiIHJ4PSIxLjE4OCIgcnk9Ii43NDIiIHRyYW5zZm9ybT0icm90YXRlKC02NS44MzQpIi8+CiAgPC9nPgo8L3N2Zz4K |
| 241 |
[awesome]: https://github.com/parro-it/awesome-micro-npm-packages |
| 242 |
[awesome-img]: https://awesome.re/mentioned-badge.svg |
| 243 |
[jquery-throttle-debounce]: https://github.com/cowboy/jquery-throttle-debounce |
| 244 |
|
| 245 |
<!-- prettier-ignore-end --> |