# Simple debouncer

**URL:** https://discourse.elm-lang.org/t/simple-debouncer/5947
**Category:** Show and Tell
**Created:** [June 21, 2020, 12:12pm UTC](https://discourse.elm-lang.org/t/simple-debouncer/5947 "2020-06-21T12:12:14Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![Laurent](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/laurent/32/2051_2.png) [@Laurent](https://discourse.elm-lang.org/u/Laurent)
#### Post date: [June 23, 2020, 5:19am UTC](https://discourse.elm-lang.org/t/simple-debouncer/5947/7 "2020-06-23T05:19:20Z")

</div>

I simply add a delayed message step to call my command only if the input is still the same after the delay. It also allows me to easily add some validation (not included in the example below for readability).

```
delayMsg : msg -> Cmd msg
delayMsg msg =
    Task.perform (always msg) (Process.sleep 1000)

-- in your update cases:

GotFieldInput fieldInput ->
    ( { model | fieldInput = fieldInput }
    , delayMsg (GotDelayedFieldInput fieldInput)
    )

GotDelayedFieldInput fieldInput ->
    if fieldInput == model.fieldInput then
        ( { model | status = Loading }, costlyCommand fieldInput )
    else
        ( model, Cmd.none )

```

I don’t see how it could be simpler 😉

---

_[View the full topic](https://discourse.elm-lang.org/t/simple-debouncer/5947)._
