Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/styllloz/public_html/qa-include/qa-base.php on line 1175

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/styllloz/public_html/qa-include/qa-base.php on line 1188

Warning: session_start(): Cannot start session when headers already sent in /home/styllloz/public_html/qa-include/app/users.php on line 162

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/styllloz/public_html/qa-include/qa-base.php on line 1175

Warning: Cannot modify header information - headers already sent by (output started at /home/styllloz/public_html/qa-include/qa-base.php:1175) in /home/styllloz/public_html/qa-include/app/users.php on line 1267

Warning: Cannot modify header information - headers already sent by (output started at /home/styllloz/public_html/qa-include/qa-base.php:1175) in /home/styllloz/public_html/qa-include/app/page.php on line 356
Module Debounce without Lodash, how to use? - code-flow.club | Q&A

Module Debounce without Lodash, how to use?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
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
35,181 users