Let's say I need to fire an function at two input events: "keyup" and "change". "Keyup" event needs to wait 3000ms. "Change" needs to wait just 500ms. If I call the debounce function in an event listener to each kind of event, the timeout id will be different and, after key up and leave the input (change), both event will be fired. So I need to share the timeout id. to do that, I put a new argument in the debounce function pointing to an external variable to be used as shared reference. It may work, but I ask, this is the best way to do it?
@cas8180282 жыл бұрын
Love it! Keep up the great work
@DevSage2 жыл бұрын
Thanks!
@alexxx44342 жыл бұрын
Very well explained. I guess the other way to solve the problem is to pass the first event though and have a cooldown when next events would be ignored.
@DevSage2 жыл бұрын
Yeah I think you're talking about throttling. Yes that's another way to prevent too many event triggers
@cjmaaz2 жыл бұрын
When the event listener gets fired multiple times when pressed multiple times, how does it remembers the last timeout Id? How does it stores the last timeout Id when it is in that scope? Is it because of closure? BTW great video, keep growing ❤️
@DevSage2 жыл бұрын
Exactly. Yes it's because of the closure. It may not be obvious just by looking at the code but technically the outer function (debounce()) only gets called once, but each time you trigger a new click the inner function gets called. Go to this link to fact check me -> playcode.io/914444 Since debounce() only gets called once, there is only one reference to the timeout ID. debounce() will remember what value was last saved in it even though the handler gets called multiple times.
@bowielam78662 жыл бұрын
That’s exactly what I want to ask. Thank you so much!
@cjmaaz2 жыл бұрын
@@DevSage Thank you so much for explaining it. Salute you for providing the playcode link.
@pauladeniyi58766 ай бұрын
Well explained. Thanks.
@eduardojavascript2 жыл бұрын
Your videos are awsome dude, thanks!
@DevSage2 жыл бұрын
You're welcome!
@shobhitkumar_shorts2 жыл бұрын
Please create video on throttling also
@NareshKushwaha-rp7rh9 ай бұрын
Superb i will be great if you make video on throttling as well.
@DevenSitapara2 жыл бұрын
Bang on concept keep it up
@shobhitkumar_shorts2 жыл бұрын
Awesome bro 🤜
@salmaneait-ssi67822 жыл бұрын
thank you so much !!
@dominiquetalis15162 жыл бұрын
Hello Devsage. I have a question. Even if the timeout is in the task queue, cleatTimeOut can delete it from the task queue. Is that right ?
@DevSage2 жыл бұрын
clearTimeout cancels a previously set timeout. So, effectively, yes it removes it from the task queue.
@dominiquetalis15162 жыл бұрын
@@DevSage Okay, thanks a lot. Your channel is a recommended place for JS developer. Keep going on !!!
@codify24502 жыл бұрын
great video, alos what is the vs extension you are using to show how many time func/var is refrenced, can you post the link please?
@codify24502 жыл бұрын
got it from the vs code setting, thanks
@Cinema69-entertainment2 жыл бұрын
Love your videos bro but i think you should change the intro sound, it is quite creepy for me