12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- export default function debounce(func, wait, immediate) {
- let timeout
- let args
- let context
- let timestamp
- let result
- const later = function later() {
- const last = +(new Date()) - timestamp
- if (last < wait && last >= 0) {
- timeout = setTimeout(later, wait - last)
- } else {
- timeout = null
- if (!immediate) {
- result = func.apply(context, args)
- if (!timeout) {
- context = null
- args = null
- }
- }
- }
- }
- return function debounced() {
- context = this
- args = arguments
- timestamp = +(new Date())
- const callNow = immediate && !timeout
- if (!timeout) {
- timeout = setTimeout(later, wait)
- }
- if (callNow) {
- result = func.apply(context, args)
- context = null
- args = null
- }
- return result
- }
- }
|