Debouncing in Javascript | Flipkart UI Interview Question

Debouncing in Javascript | Flipkart UI Interview Question

Akshay Saini

5 лет назад

219,789 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@vishalbisht6871
@vishalbisht6871 - 28.02.2020 20:40

Hi Akshay,
Because of your true efforts to learn and let others learn, i today got selected in amazon. Thanks a lot 🙏

Ответить
@amitkumargouda1768
@amitkumargouda1768 - 18.12.2023 17:05

ES6 version of this solution :
let counter = 0;
const fetchFunc = (e) => {
console.log("Fetching data...", counter++,e);
};

const debounce = (functionRef, timeInMS) => {
let timer;
return (e) => {
clearTimeout(timer);
timer = setTimeout(()=> functionRef(e), timeInMS);
};
};

const debounceFunc = debounce(fetchFunc, 300);

Ответить
@coolme7437
@coolme7437 - 12.12.2023 00:15

2023 Attendace

Ответить
@ikik790
@ikik790 - 13.11.2023 17:20

completed

Ответить
@shellyshack9364
@shellyshack9364 - 10.11.2023 15:19

What about when the user copy and paste the content on the search, waiting is not necessary in this case. How can we solve this? Should we have another event listener for paste and update the debounce timer?!

Ответить
@shreyasikhar1044
@shreyasikhar1044 - 26.10.2023 08:44

// Those who have faced issue while trying to use function arguments inside debounce, try this:
const getData = ( time ) => {
console.log('fetched data at time: ' + time );
}
const debounce = (fn, d, args) => {
let timer;
return () => {
let context = this;
clearTimeout( timer );
timer = setTimeout(() => {
fn.apply( context, args );
}, d);
}
}
const debouncedGetData = debounce( getData, 300, [ new Date() ] );
// call debouncedGetData() on keyup event.

Ответить
@ishumishra8104
@ishumishra8104 - 15.10.2023 15:56

i dont think so in this case .apply() will gets called as we dont have any object or anything to call it under.
Directly call fn() under setTimeout

Ответить
@ghnshym143
@ghnshym143 - 06.10.2023 10:15

<input type="text" onkeyup="betterfunction()">

<script>
let count = 1;
function getData() {
//here you can call api to get data.
console.log("Hello!", count++);
}

function debounce(fun, delay) {
let timer;
return function () {
clearTimeout(timer);
timer = setTimeout(fun, delay);
}
}

const betterfunction = debounce(getData, 500);
</script>
there's a small issue
Use the provided function, not getData();

return function () {
clearTimeout(timer);
timer = setTimeout(fun, delay);
}
Thank You😇😊

Ответить
@dhruvthakkar4350
@dhruvthakkar4350 - 05.10.2023 18:00

I dont understand this. My below code is giving the same result. Why needed cleartimeout ?

<html>
<input id="name" type="text" onkeyup="abc()" />
</html>
<script>
function abc() {
let xx;
xx = setTimeout(() => {
console.log(document.getElementById("name").value);
}, 300);
}
</script>

Ответить
@chandrachurmukherjeejucse5816
@chandrachurmukherjeejucse5816 - 24.09.2023 12:40

Great lecture. Minor correction: inside debounce method you have used getData.apply(), it should be fn.apply() as the parameter is fn

Ответить
@affansajjad4095
@affansajjad4095 - 15.09.2023 21:06

That was helpful

Ответить
@affansajjad4095
@affansajjad4095 - 15.09.2023 21:06

Thanks man.

Ответить
@shivamaditya8711
@shivamaditya8711 - 19.08.2023 19:25

sir please make video on async and await.

Ответить
@rootbattlegrounds7948
@rootbattlegrounds7948 - 26.07.2023 15:57

Created some unused variables to make it look complex

Ответить
@Prakash_399
@Prakash_399 - 09.07.2023 09:24

Its true that its all about how we think logically while coding. Its still confusing for me why we need to clear the timer here.

Ответить
@random2059
@random2059 - 03.07.2023 13:28

🙏🙏🙏🙏 please reply
so every time we go inside dosome magic
Let Timer ;
reinitialize or holds the previous value ???
Or js initialize timer only one time 1st time after that how many time it sees. let timer ; it does not care
this concept is important

Ответить
@CleverSuree
@CleverSuree - 21.06.2023 18:40

jab se maine Akshay bhaiya k namaste javascript video ko dekhna start kra exdm se love hone lga javascript mein.
Ab main kitne bhi hours javascript mein code kru mereko boring ni lgta.
Thankyou so much bhaiya for making these kinda videos.
very nice explanation agian :) 😅😅

Ответить
@057shubhamgoyal2
@057shubhamgoyal2 - 19.05.2023 23:44

Great explanation. I am bit confused about the role of arguments and context here. How can we use them? Please explain.

Ответить
@Gabarde25gmailcom
@Gabarde25gmailcom - 07.05.2023 14:46

Hello Akhay, great video and got to learn a lot as always, but I have a doubt, I'm able implement the same functionality with below code:
let timer;
const getData = () => {
clearTimeout(timer);
timer = setTimeout(() => {
console.log("Fetching Data ...");
}, 300);
};
and one more doubt, why we cannot invoke getData function by fn(); directly, instead of fn.apply(contest, args), am I missing something?

Ответить
@girikgarg8
@girikgarg8 - 28.02.2023 05:26

Anyone can explain, why we have used this and args in the function?

Ответить
@AbhishekSharma-ui1yb
@AbhishekSharma-ui1yb - 14.02.2023 07:30

Hi Akshay, I think on line no 14 we shouldn't call directly getData we already have a parameter name "fn"

Ответить
@mrnabby4178
@mrnabby4178 - 24.01.2023 16:35

Crystal clear bro. Thank you bro.

Ответить
@bharatnair6919
@bharatnair6919 - 05.01.2023 16:47

Hi @Akshay and everyone, I tried using this [without using apply, args and this] and it seems to be working, any problem with this approach ?
const getData = () => {
console.log("//api call");
};

const debounce = (fun, d) => {
let timer;
return function () {
clearTimeout(timer);
timer = setTimeout(() => {
fun();
}, d);
};
};

const betterFunction = debounce(getData, 500);

Ответить
@RethinkingUI
@RethinkingUI - 27.12.2022 20:38

Superb

Ответить
@ashasr8640
@ashasr8640 - 10.11.2022 08:06

Hey It is an amazing video to learn on debouncing. Thanks a lot

Ответить
@visheshpanchal306
@visheshpanchal306 - 31.10.2022 07:19

Sir, please add nodejs interview question like process.nexttick and setimmediate. like questions.

Ответить
@brijdi389
@brijdi389 - 30.10.2022 10:04

In case if anyone looking for shorter version:

const debounce = (fn, wait) => {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(fn, wait, ...args);
}
}

Ответить
@maruthiprasad1452
@maruthiprasad1452 - 23.10.2022 20:19

Thank you Akshay...

Ответить
@aadriovijay7244
@aadriovijay7244 - 16.10.2022 18:17

Thanks askay .. clearly understood

Ответить
@yashjain1492
@yashjain1492 - 08.10.2022 12:57

why are we returning a function in dosomemagic , can't we directly write time out code in outer function?

Ответить
@ShivamKumar-oz4oo
@ShivamKumar-oz4oo - 22.09.2022 10:05

@Akshay Saini instead of calling getData hardcoded function in debounce function we can call the arguments fn

Ответить
@anshikapathak4422
@anshikapathak4422 - 18.09.2022 06:01

Hello Akshay. I understood everything but what is "FN" people are commenting here. I really don't understand use of fn?

Ответить
@maxwilsonpereira
@maxwilsonpereira - 08.09.2022 14:27

Hello Akshay! Thanks a lot for your tutorials! They are amazing!
I have a question: Why do you and many sites on the internet show this example using ARGS and THIS, if you could simplify and make something like this that is much easier and straightforward to understand. This is tested and working! What am I missing here?

import * as React from 'react';
export default function App() {

function debounce(cbFunc, timeout = 300){
let timer;
return () => {
clearTimeout(timer);
timer = setTimeout(() => {
cbFunc()
}, timeout);
};
}

function fetchDataHandler(){
console.log('Fetching data from API');
}

const fetchData = debounce(() => fetchDataHandler(), 500);

return (
<div>
<h2>Search API</h2>
<input onChange={fetchData}/>
</div>
);
}

Ответить
@rajkumarjhalani1145
@rajkumarjhalani1145 - 19.08.2022 11:21

Yesterday I couldn't answer this question in an interview and now m learning from your videos!

Ответить
@RegiiPad
@RegiiPad - 29.07.2022 06:27

why args if you're not passing it on fn.apply?

Ответить
@abhisheksatyam4733
@abhisheksatyam4733 - 16.07.2022 19:26

Can we not write doSomeMagic as a arrow function ?

Ответить
@asifurrahman5436
@asifurrahman5436 - 04.07.2022 19:45

thank you so much akshey vai

Ответить
@sumitsinha995
@sumitsinha995 - 19.06.2022 07:32

bhaiya every video is new eye opener in itself!

Ответить
@vinaysharma3871
@vinaysharma3871 - 15.06.2022 22:40

What if you don't use apply?

Ответить
@sukeshkumar-dh5rm
@sukeshkumar-dh5rm - 13.06.2022 15:59

Hello akshay,

I tried implementing the same with multiple arguments in angular 8. But event is firing upon calling debounce function

Ответить
@meenabagwan2251
@meenabagwan2251 - 13.06.2022 15:22

Amazing explanation @Akshay Saini 🙌
I have query:- You have passed args like
args = arguments :- Are that the arguments which we can pass in the getData function?
I have tried to pass some arguments and tried to access it but it doesn't work

Ответить
@narendrareddyyarramreddy2007
@narendrareddyyarramreddy2007 - 11.06.2022 08:49

let is block scoped. How does clearTimeout(timer) have a reference to a previously created timer? Can anyone clear my doubt please

Ответить
@rajraju4761
@rajraju4761 - 10.05.2022 22:12

Very good explanation @Akshay. However there are few bugs and unused variables in the code.
1. You haven't used 'fn' param in debounce function, though you have passed the 'argument' - 'getData' from 20th line.
2. If you had used the 'fn' param in the setTimout function, you would have avoided the getData function in line no.15
3. If you had adjusted the 2nd point right, you wouldn't need to keep the reference of 'this' and 'arg' unnecessarily, It would have automatically keeps the outer function params value intact. ( That's what closure does).

Ответить
@sarangbelsare7176
@sarangbelsare7176 - 22.04.2022 15:51

Hi Akshay, can you make a more detailed video on call,apply and blind for binding this context when used inside currying function.

Ответить
@GouravKumar-qq1fb
@GouravKumar-qq1fb - 18.04.2022 04:16

Let contex = this and args = argument? Can someone help me in the

Ответить
@chengjinfei8139
@chengjinfei8139 - 08.04.2022 02:40

If you are confused why timer variable need to defined outside of the anonymous function, that is closure !!!

Ответить