Skip to content

_.debounce loops indefinitely if called without “wait” argument #991

@bogdanb

Description

@bogdanb

lodash.src.js (as of commit 5613f60) has this at line 7437 (the beginning steps of _.debounce):

  wait = wait < 0 ? 0 : wait;

If you call debounce without a wait argument, i.e. _.debounce(f), the variable wait remains undefined. A bit later (starting at line 7457) there is this:

  function delayed() {
    var remaining = wait - (now() - stamp);
    if (remaining <= 0 || remaining > wait) {
      // cleanup and call debounced function 
    } else {
      timeoutId = setTimeout(delayed, remaining);
    }
  } 

Now, because wait is undefined, the value of remaining becomes NaN, and the test right below it is always false. Thus the delayed function is set as a timeout again, which is does the same thing again, etc. Basically it loops indefinitely.

I propose changing line 7457 to

  wait = wait >= 0 ? +wait : 0;

which forces wait to be zero if it is undefined.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions