javascript – React debounce using Lodash

I need people who have experience with React to review my code.

I want to make a debounce using Lodash.

import debounce from 'lodash/debounce'
const (name, setName) = React.useState<string | undefined>(undefined)
const (displayedName, setDisplayedName) = React.useState<string | undefined>(undefined)

const changeName = debounce((e: domProperty) => {
}, 1000)

React.useEffect(() => {
  // call API
}, (name))
return (
    placeholder="Player name"
    prefix={<UserOutlined />}
    onChange={(e) => {

Is it good? or is there anything wrong there?

So here I use displayedName because when I type something in Input, the name state doesn’t change as it might be blocked by a debounce.

For example: when I type “alex” at the beginning (it works), then when I want to add the letter “i” (it doesnt, so I can’t add or remove any letter it always “alex”), the name state is still the same as “alex”. Therefore I add displayedName.

Mix JavaScript debounce (not plugin) with jQuery event handler?

I asked this on StackOverflow and was directed here…

Instead of including a debounce plugin library for a very small form section of my site, I’ve implemented some of David Walsh’s JavaScript Debounce Function in my code to allow a small wait for slower user input, however I am binding it to the input field using jQuery instead of regular DOM event listener. The code works fine but my main question here is some peer input on if this is ok to do (will it cause unforeseen issues? is there a better method?) I couldn’t find another example on this scenario on stack overflow but if it has been already asked, please do point me in the right direction.

$(function() {

    // debounce function by David Walsh
    function debounce(func, wait, immediate) {
        var timeout;
        return function() {
            var context = this, args = arguments;
            var later = function() {
                timeout = null;
                if (!immediate) func.apply(context, args);
            var callNow = immediate && !timeout;
            timeout = setTimeout(later, wait);
            if (callNow) func.apply(context, args);

    // allow .5s for slower user input before processing
    var debounceCustomInput = debounce(function() {
        // if both inputs valid
        if ($("#left").val().length > 0 && $("#right").val().length > 0) {
            // process form
    }, 500);

    // event binding
    $(document).on("input", "#left,#right", function() {
        // debounce input

javascript – Which debounce function is better, and why?

The debounce() function forces a function to wait a certain amount of time before running again…

Function 1

function debounce(callback, interval) {
  let lastTime = -Infinity;
  return function(){
    let now =, res;
    if (now - lastTime > interval) {
      res = callback();
    lastTime = now;
    return res;

Function 2

function debounce(callback, interval) {
  let timeout = null;
  return function(){
    let res;
    if (!timeout) { res = callback(); }
    timeout = setTimeout(() => timeout = null, interval);
    return res;

Both functions produce the same result but I want to know which implementation is better and why, as I’m currently studying JavaScript to improve my code quality.

function hello() { return 'hello'; }
const sayHello = debounce(hello, 3000);

console.log( sayHello() ); // -> 'hello'
setTimeout(function() { console.log( sayHello() ); }, 2000); // -> undefined
setTimeout(function() { console.log( sayHello() ); }, 4000); // -> undefined
setTimeout(function() { console.log( sayHello() ); }, 8000); // -> 'hello'

assembly – Debounce counter for PIC micro – Suggestions for improvement

I developed this code for a vertical debounce counter for the PIC micro (PIC16F57). The code is called every 512 microseconds. It takes 5 bits from PORTB and compares it to a last known debounced state. If an active input has been stable for 64 calls it is accepted as changed. Though it is sufficiently good for the current application, I am interested in possible improvements to speed up overall execution time. I am not interested in prematurely leaving the loop in case there is nothing to update.

                        cblock              0x08
gpcounter:1                                         ; general purpose counter

    ; Button debounce
                            ; A button state is considered stable if it has not changed in 64 polls each 512 usec
        debounce            ; Read input state. Only bits 0-4 are inputs
                            movf            PORTB, W
                            andlw           0x1f
                            ; Any input bits that have changed with respect to the last known state
                            ; will be set to 1's in delta and toggle.
                            ; delta remains fixed, toggle will act as a carry while looping
                            ; Because toggle bits will be reset only, toggle will always be a subset of delta.
                            xorwf           debstate, W
                            movwf           delta
                            movwf           toggle
                            ; Load first counter row in index register
                            movlw           vcounter
                            movwf           FSR
                            ; Counter has 6 rows 
                            movlw           0x06
                            movwf           gpcounter
        debounceloop        ; Reset row for all bits which  have not changed states
                            movf            delta, W
                            andwf           INDF, F
                            ; Flip row bits according to toggle mask.
                            ; Because the 1's in toggle are a subset of those in delta, 
                            ; any reset counter bits can never be toggled back to 1
                            movf            toggle, W
                            xorwf           INDF, F
                            ; If any bits were toggled from 1 to 0, retain carry bit, else discard it
                            comf            INDF, W
                            andwf           toggle, F
                            ; We're done with this row
                            incf            FSR, F
                            decfsz          gpcounter, F
                            goto            debounceloop
                            ; toggle will now contain 1's for any counters
                            ; that have overflown, so it can be used as xor mask
                            ; to update the last known debounced state
                            movf            toggle, W
                            xorwf           debstate, F
                            retlw           0x00

sharepoint online – Debounce in sfx react

    this.handleCommChange (e, & # 39; contact & # 39;)}

private handleCommChange (e: any, controlType: any): void {
const {temp_items} = this.state;
const value = e;

this.setState ({
temp_items: {
... temp_items,
        [controlType]: Value
console.log (this.state);

If you want to react to Debounce in SPFX, respond.

Please help

Many Thanks.

Forms – How long should the debounce time limit be?


Get feedback from users, Receive feedback from usersget feedback from users. 500ms is a good starting point.

Your answer will most likely vary depending on the purpose and platform on which it is run. In general, you want to give feedback to the user as soon as possible about doing something wrong (an incorrectly formatted e-mail address would be a good example). The benefit of quick feedback on form validation is also that they know immediately which field is not filled out properly (instead of completing the form and then being notified that a field is missing somewhere).

So let's look at different use cases and platforms and possible pitfalls if your debouncing is too short or too long.

Problems with too short debouncing

If you ran a debounce in less than 200 ms for the average user, it will be displayed almost immediately. The result would be a user who is just starting to enter his e-mail address, and he will be told immediately that he has the wrong format. Imagine when you were a kid and your parents wiped the floor, and before they're done, say, "Miss a spot!" … you will not take that too nicely. Similar concept here.
Moral of the story, do not make your debouncing too short. Give your user some time to complete the field.

Problems with too long debouncing

Now consider the other side of the time spectrum. You fill in the e-mail field again, and it is the last field in the form. You type away (but for your information, you typed incorrectly), quit the form, click Submit and wait while the form is submitted … Only then will the "Womp Womp" error be displayed.
This is too long a wait. The user entered a field and their focus has been moved further. If you need to drag the user back into a box, you feel he's not making any progress (you want forms to have a sense of progress, so the user can know the tasks are complete).
Stick to the example of housework. In that case, imagine cleaning your room and bathroom. They quit their room and are halfway to the bathroom when their parents come in and say they've cleaned the room in a terrible way (it turns out you can not just throw everything under the bed). Now you have to go back in your room, only to go then back to clean the bathroom. Does not feel like progress.


So we have our two ends of the spectrum.
At one end we have the jabber mouth "You are doing everything wrong" debounce.
And at the other end we have the "Hey, come back" debounce.
Both are not desirable. Rather, we want debunking, which serves as a guide. When we complete the form, we learn (after completing the task) whether we did it right or not. We finish the bed and then our parent tells us that we have done good / horrible work.
So that's the problem we want to solve.


Remember that users have different writing speeds. Some people will probably be at the low end of 20 words per minute while others will burn fast at over 100. When you work with mobile, people tend to type slower on mobile than on a keyboard (I would still type that in if I was on my phone …). All these things affect your debounce time.

If your debounce time must vary significantly due to different write speeds, other approaches may be applicable (for example, a type & # 39; onFieldLeave & # 39;).

Without information about what type of platform / audience you are addressing, Half a second (500ms) can be helpful starting point, They want to give the user time to fill in a field, but they attract his attention before they pass him by. 2 seconds (2000 ms) may be too long, depending on how fast people are typed, and 200 ms would be a blink of an eye (both situations we've already covered).

As suggested by @SteveD in the comments, You really should have some user testing to determine which debounce time is best for your application, Have a few different times and get feedback. Nothing beats the cold, hard answers you get from user feedback.

Here is a short article that gives you an idea of ​​the reaction times of people who might be enlightening.

Update 1

There is a useful website that documents some basic UX laws ( Look at the Doherty Threshold, which can be useful for measuring interaction time.