Module Debounce without Lodash, how to use?

0 like 0 dislike
5 views
Here's a module I copied somewhere
/helpers/debounce.js
module.exports = function debounce (fn, delay) { var timeout; return function() { var ctx = this, args = arguments; clearTimeout(timeout); timeout = setTimeout(function() { fn.apply(ctx, args); }, delay); }; }


I use it like this:
var debounce = require('./helpers/debounce.js')
and inside the method write:
debounce(this.test, 1000)('N')
It works, but I am confused that is usually written differently:
debounce(this.test('N'), 1000)
But it didn't work.
Please explain how to do it right!
by | 5 views

3 Answers

0 like 0 dislike
You're doing it wrong.
Correctly so:
var handler = function(e) { // some actions } var debouncedHandler = debounce(handler, 2000); someElement.addEventListener('keyup', debouncedHanlder);

Calling debounce returns a function handler, which you should use, and every time you created it.

The method of using is neither in Vue, nor React, nor anywhere else.
by
0 like 0 dislike
And indeed strange debounce, if I call it several times during the delay, he also calls the function multiple times with a specified delay, and was supposed to call only once?
by
0 like 0 dislike
by

Related questions

0 like 0 dislike
1 answer
asked Apr 20, 2019 by kirill-93
0 like 0 dislike
1 answer
asked Jun 8, 2019 by nikrull
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
33,949 users