Добавил debouncing

This commit is contained in:
Sipachev Igor 2023-01-24 19:00:44 +07:00
parent 7a84ff4a74
commit ec41e91388

18
api/debouncing.ts Normal file
View File

@ -0,0 +1,18 @@
export const debouncing = async (time: number = 1000) => {
return new Promise((resolve, reject) => {
const timerId = setTimeout(() => {
resolve(true)
}, time)
const listener = (event: any) => {
if (event.detail === timerId) {
return
}
clearTimeout(timerId)
reject(new Error('locked!!!' + timerId))
document.removeEventListener('debiuncing.clean', listener);
}
document.addEventListener('debiuncing.clean', listener)
const event = new CustomEvent('debiuncing.clean', { detail: timerId });
document.dispatchEvent(event);
})
}