Skip to content

Commit

Permalink
Update F.debounce and F.throttle
Browse files Browse the repository at this point in the history
  • Loading branch information
mobily committed Dec 11, 2021
1 parent aa1b767 commit 2545d74
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/Function/Function.res
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export throttle = (fn, delay) => {
Belt.Option.mapWithDefaultU(timer.contents, (), (. timer) => Js.Global.clearTimeout(timer))
timer := None
}
let make = () =>

(. ()) =>
if !isThrottled.contents {
cancel()
isThrottled := true
Expand All @@ -75,24 +76,21 @@ export throttle = (fn, delay) => {
}, delay)
timer := Some(timeout)
}

make
}

let debounce = (fn, delay) => {
export debounce = (fn, delay) => {
let timer = ref(None)
let cancel = () => {
Belt.Option.mapWithDefaultU(timer.contents, (), (. timer) => Js.Global.clearTimeout(timer))
timer := None
}
let make = () => {

(. ()) => {
cancel()
let timeout = Js.Global.setTimeout(() => {
fn()
timer := None
}, delay)
timer := Some(timeout)
}

make
}

0 comments on commit 2545d74

Please sign in to comment.