# Debounce notfication of an Observable

**URL:** <https://discourse.julialang.org/t/debounce-notfication-of-an-observable/111079>\
**Category:** General Usage\
**Created:** [March 3, 2024, 12:50am UTC](https://discourse.julialang.org/t/debounce-notfication-of-an-observable/111079 "2024-03-03T00:50:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bryaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bryaan/32/36858_2.png) [@bryaan](https://discourse.julialang.org/u/bryaan)\
**Post date:** [March 3, 2024, 12:50am UTC](https://discourse.julialang.org/t/debounce-notfication-of-an-observable/111079/1 "2024-03-03T00:50:53Z")

</div>

Is there a simple way to debounce a function call like in js’ Lodash?

I want to notfiy an Observable at most once every 0.25s.

This is useful for performance reasons in Makie.

```julia
obs = Observable(1)
notify(obs, debounce=0.25)

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [March 3, 2024, 1:37am UTC](https://discourse.julialang.org/t/debounce-notfication-of-an-observable/111079/2 "2024-03-03T01:37:36Z")

</div>

According to my quick search, you might be looking for `async_latest`. The docs link is:  
[async\_latest doc](https://juliagizmos.github.io/Observables.jl/stable/#Observables.async_latest-Union%7BTuple%7BAbstractObservable%7BT%7D%7D,%20Tuple%7BT%7D,%20Tuple%7BAbstractObservable%7BT%7D,%20Any%7D%7D%20where%20T:~:text=Observables.async_latest)

You would write:

```julia
obs = Observable(1)
async_latest(obs)

```

The example in the docs is specifically relevant to updating graphics.

---

<div class="post-metadata">

**Author:** ![bryaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bryaan/32/36858_2.png) [@bryaan](https://discourse.julialang.org/u/bryaan)\
**Post date:** [March 3, 2024, 2:01am UTC](https://discourse.julialang.org/t/debounce-notfication-of-an-observable/111079/3 "2024-03-03T02:01:24Z")

</div>

Does not seem to do the trick in my case. This looks like it requires a debounce.

This is my attempt at a debounce function but the function has something wrong with it:

```julia
function debounce(func::Function, wait::Float64)
    timer = nothing
    function debounced_func()
        if timer === nothing
            timer = Timer(wait) do
                (func)()
                timer = nothing
            end
        end
    end
    return debounced_func
end

debounced_notify = debounce(0.50) do
      notify(...)
end

debounced_notify()

```

Maybe it would work if I could use async\_latest on this:

```julia
    onmouseleftdrag(mouseevents) do event
        notify(...)
    end

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 3, 2024, 8:46am UTC](https://discourse.julialang.org/t/debounce-notfication-of-an-observable/111079/4 "2024-03-03T08:46:45Z")

</div>

See [`Observables.throttle`](https://juliagizmos.github.io/Observables.jl/stable/#Observables.throttle-Union%7BTuple%7BT%7D,%20Tuple%7BAny,%20AbstractObservable%7BT%7D%7D%7D%20where%20T) .
