thanks for showing real world examples since they are very useful and makes the problem practical
@victornorman3671
7 ай бұрын
This was a really good tutorial, the perfect amount of depth and good examples, 10/10. Keep up the great work!
@Arttturslv
5 ай бұрын
I spend so much time thinking how to identify if the call was already made, but the solution was so simple lol
@Android-17
Жыл бұрын
Wow I also didn't know we could pass undefined in clearTimeout😟 Thank you!
@Amar11115
Жыл бұрын
It was the most simple explanation.
@diwakardayal954
6 ай бұрын
clean thanks
@krateskim4169
Жыл бұрын
Thank you so much
@TheMarkBrut
Жыл бұрын
thanks
@tsunghan_yu
Жыл бұрын
I know it's not needed but why does passing in ...args to the callback function fail? I have to use `() => fn(...args)` instead. var debounce = function(fn, t) { let timeoutId; return function(...args) { clearTimeout(timeoutId); timeoutId = setTimeout((...args) => fn(...args), t); } }; EDIT: it's because setTimeout lets you call the callback with optional parameters and we didn't pass them in. Like this `setTimeout(functionRef, delay, args)`, but the callback function `(...args) => fn(...args)` doesn't work and it has to be (args) => fn(...args).
@vixguy
Жыл бұрын
Day 15 of doing the 30-day challenge with neetcode
@learnerforlife1338
Жыл бұрын
i did apply almost the same logic but instead of always calling the clearTimeout i used an IF statement. But i don't know why it isn't giving the desired output. Code: var debounce = function(fn, t) { var called = false; var id; return function(...args) { if(called){ called = false; clearTimeout(id); }else{ id = setTimeout(()=>fn(...args),t); called = true; } } };
@kartikeyrana3736
Жыл бұрын
suppose we have two calls c1 and c2 with t = 100ms, c1 being called at 10 ms and c2 being called at 30 ms. Here when c1 call is made the called var becomes true and id is set, now when you call c2, as the called var is true it goes to the if block where you are setting the called var as false which is wrong rather you have to keep the called var as true(meaning there is no need for var called so remove it) for the c2 call and register its timeout in id. cleaned code : var debounce = function(fn, t) { let cache = null; return function(...args) { return new Promise((res, rej)=>{ if(cache != null) clearTimeout(cache); let timeout = setTimeout(()=>{ cache = null; res(fn(...args)); }, t) cache = timeout; }) } };
@seanmcelroy4074
Жыл бұрын
remove the else. You want to set a new timer every time even when your clearing the previous timer
@jaylee7165
Жыл бұрын
Correct me if i'm wrong, but we still have to return the id at the end?
Пікірлер: 15