throttled
Create a throttled function, which calls the given function f
at most once per given interval max_delay
.
It is leading (f
is invoked immediately) and not trailing (calls during a cooldown period are ignored).
👀 Reading hidden code
"Create a throttled function, which calls the given function `f` at most once per given interval `max_delay`.
It is _leading_ (`f` is invoked immediately) and _not trailing_ (calls during a cooldown period are ignored)."
function throttled(f::Function, max_delay::Real, initial_offset::Real=0)
# return f
() -> begin
now = time()
end
nothing
end
end
log (generic function with 1 method)
👀 Reading hidden code
function log()
@info "Hello" rand()
end
👀 Reading hidden code
log()
Hello
0.3362369120617531
#1 (generic function with 1 method)
t = throttled(log, 2, 5)
👀 Reading hidden code
t()
👀 Reading hidden code
0.03041815757751465
let
a = time()
b = sqrt.([200])
time() - a
end
👀 Reading hidden code
With cooldown delay
👀 Reading hidden code
throttled2
Create a throttled function, which calls the given function f
at most once per given interval max_delay
.
It is leading (f
is invoked immediately) and not trailing (calls during a cooldown period are ignored).
An optional third argument sets an initial cooldown period, default is 0
. With a non-zero value, the throttle is no longer leading.
"Create a throttled function, which calls the given function `f` at most once per given interval `max_delay`.
It is _leading_ (`f` is invoked immediately) and _not trailing_ (calls during a cooldown period are ignored).
An optional third argument sets an initial cooldown period, default is `0`. With a non-zero value, the throttle is no longer _leading_."
function throttled2(f::Function, max_delay::Real, initial_offset::Real=0)
local last_run_at = time() - max_delay + initial_offset
# return f
() -> begin
now = time()
if now - last_run_at >= max_delay
f()
last_run_at = now
end
nothing
end
end
👀 Reading hidden code
👀 Reading hidden code
👀 Reading hidden code