You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
734 B
38 lines
734 B
export default (callback: () => boolean, buffer: number) => {
|
|
let called = false;
|
|
let timeout = null;
|
|
|
|
function cancelTrigger() {
|
|
window.clearTimeout(timeout);
|
|
}
|
|
|
|
function trigger(force?: boolean) {
|
|
if (!called || force === true) {
|
|
if (callback() === false) {
|
|
// Not delay since callback cancelled self
|
|
return;
|
|
}
|
|
|
|
called = true;
|
|
cancelTrigger();
|
|
timeout = window.setTimeout(() => {
|
|
called = false;
|
|
}, buffer);
|
|
} else {
|
|
cancelTrigger();
|
|
timeout = window.setTimeout(() => {
|
|
called = false;
|
|
trigger();
|
|
}, buffer);
|
|
}
|
|
}
|
|
|
|
return [
|
|
trigger,
|
|
() => {
|
|
called = false;
|
|
cancelTrigger();
|
|
},
|
|
];
|
|
};
|