Javascript Input Text Change Delay Timeout

August 21, 2018
let timeout = null

$("#text-input").on('input', function() {
    const val = $(this).val()

    let delay = 300
    const len = val.length
    if (len == 0) {
      return
    }
    else if (len == 1) {
      delay = 1000
    }
    else if (len <= 3) {
      delay = 700
    }
    else if (len <= 5) {
      delay = 500
    }

    clearTimeout(timeout)
    timeout = setTimeout(() => {
        // do something
    }, delay)
})
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.