Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_.debounce() loses event.currentTarget #1905

Closed
jezen opened this issue Oct 23, 2014 · 8 comments
Closed

_.debounce() loses event.currentTarget #1905

jezen opened this issue Oct 23, 2014 · 8 comments

Comments

@jezen
Copy link

jezen commented Oct 23, 2014

I am unable to get the event.currentTarget when using addEventListener with _.debounce().

Here’s a fiddle.

@jdalton
Copy link
Contributor

jdalton commented Oct 23, 2014

If the property is changed on the event object reference that's outside the scope of Underscore. If you use a lib like jQuery that may help as they make a synthetic event object which you should be able to reference without issue.

@jridgewell
Copy link
Collaborator

I don't think you're going to get around this. event is reused as it propagates up the DOM, with currentTarget changing for each element with a listener. fiddle

Passing a second parameter can get around this. fiddle

var debounced = _.debounce(fn, 50);
document.querySelector('.debounced-input').addEventListener('keyup', function(event) {
    debounced(event, event.currentTarget);
});

@megawac
Copy link
Collaborator

megawac commented Oct 23, 2014

Yep, this is outside of the scope of underscore. Consider a DOM event micro framework like gator

@megawac megawac closed this as completed Oct 23, 2014
@jdalton
Copy link
Contributor

jdalton commented Oct 23, 2014

Here's an example of it working with jQuery.

@jezen
Copy link
Author

jezen commented Oct 23, 2014

I can still get the element that currentTarget usually has with this; I just opened this issue to make sure what I’m seeing is actually expected behaviour.

@samrispaud
Copy link

@jridgewell any idea how to get around this in a React onChange. when i don't use debounce I am able to access the proper event in my onChange function but when I use _.debounce I lose the proper event – see below

// in my render
<input onChange={ _.debounce(this.updateNote.bind(this), 500) } />
// my change handler
updateNote(event) {
    // undefined on event.target, get lodash event not <input/> event target
    const note = event.target.value
    this.sendNote({ note })
 }

@jridgewell
Copy link
Collaborator

See my second suggestion.

@jacobhobson
Copy link

jacobhobson commented Sep 15, 2018

This is a matter of implementation. You need to persist the event. onChange should call a persist and THEN pass the persisted event to the debounced handler:

onChange_persist = (e) =>
{
    e.persist();
    e.onChange_debounced(e);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants