• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home » Resolved: calling function in difference ways

Resolved: calling function in difference ways

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

what is difference between
and
if I import this file and call it three times tandemly, in the first one func will be called tree times after 3sec, and at second one it will be called just once after 3sec. but I dont know why!
actually I want a function to which I can pass many different callbacks, and it will run only the last one,but i dont want to get my apiCallFunk with importing it like the second one. i want my apiCallFunk as argument. because of this I have to use first one.
the use:

Answer:

The code with your first snippet is calling debounce three times, creating three different yui functions that are debounced individually (each of them being a closure with its own let timer), and then calls each of them once.
The code with your second snippet is calling debounce once, then calls that debounced function three times, causing the timer mechanism to prevent two of the logs.

Actually I want a function to which I can pass many different callbacks, and it will run only the last one, because of this I have to use first one.


No, you need the second approach where debouce is only called once and a single timer is created. What you really want is a debounce function that passes through arguments:
However, you should not export that debounce() call, it creates a module-level state and it will be impossible to create multiple different debounced functions with separate state or timeout lenghts (e.g. for multiple instances of the component). Calling debouce immediately and exporting the result is the same as writing a module
Don’t do that. Instead, export the debounce function itself, and then call it in your component where onSearch is needed:

If you have better answer, please add a comment about this, thank you!

javascript typescript
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Use Svelte Component as Slot

26/03/2023

Resolved: Vaadin 14.9 – Redirect on Session Destroy Event

26/03/2023

Resolved: How to separate multiple answers in one column, for multiple columns, by creating extra columns

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.