Debouncing
We guys must have played The Bounce Game in Nokia! In fact, I could say like, it is the first adventurous game I have played! The ball there bounces once hits a wall! In Electronics, If we have a switch or a button, when we turn it on, it may bounce back and forth, unwantedly causing some electrical touches and we could see the Bulb being glowing and offing frequently. We can control such button design by something called Debouncer. If you guys have interest in Electronics, here you go to explore The SR Latch which avoids the bouncing of switches!
Now, letβs come to our case! When a user clicks on a button multiple times accidentally or intentionally(π), we may need to handle those cases! A Button click may induce an API invocation!
If he/she clicks 6 times in a second like the up and down motion of beeβs wings, well! The API threshold limit will be exceeded and nothing we can do! Where is my button? Here π is!
π is clicked ββ> API Request Sent
0.20 seconds
π is clicked ββ> API Request Sent
0.20 seconds
π is clicked ββ> API Request Sent
0.20 seconds
π is clicked ββ> API Request Sent
0.20 seconds
π is clicked ββ> API Request Sent
0.20 seconds
π is clicked ββ> API LIMIT REACHED
So, how can we get rid of this? How can we control this bouncing effect? Actually, our UI buttons are working properly.. By bouncing, we mean this continuous invocations of API! So, how can we? Simple!
Let me explain.. We need a timer to achieve this debouncingβ¦ HERE βοΈ is!
Now, here is how we can implement the debouncing!
π is clicked β-> β SET FOR 3 SECONDS β-> API Request will be sent after 3 SECONDS
0.20 seconds
π is clicked β-> Previous Timer is cleared & New β SET FOR 3 SECONDS β-> API Request will be sent after 3 SECS
0.20 seconds
π is clicked β-> Previous Timer is cleared & New β SET FOR 3 SECONDS β-> API Request will be sent after 3 SECS
0.20 seconds
π is clicked β-> Previous Timer is cleared & New β SET FOR 3 SECONDS β-> API Request will be sent after 3 SECS
0.20 seconds
π is clicked β-> Previous Timer is cleared & New β SET FOR 3 SECONDS β-> API Request will be sent after 3 SECS
0.20 seconds
π is clicked β-> Previous Timer is cleared & New β SET FOR 3 SECONDS β-> API Request will be sent after 3 SECS
AFTER 3 SECONDS ββ> API Request Sent
Hey Hey! We made it! So, letβs code it! π Itβs applicable for something like the Input Boxes in React also! Where we set to perform something intensive for each changes! Remember! Itβs not only for API calls! Itβs for any intensive operations!