
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain javascript debounce function?
A debounce function limits the rate at which a function can fire. For example, an autocomplete text bar that queries the server. If you query the server at each keystroke, it'll be unnecessarily have network and memory impact. What you can instead do is limit the number of these calls going in a given amount of time.
You can write your own debounce function that takes your actual function as an argument and executes it in a rate limited(throttled) way.
Example
const debounce = (cb, time) => { let timeout; return function() { const wrapperFunc = () => cb.apply(this, arguments); clearTimeout(timeout); timeout = setTimeout(wrapperFunc, time); } }
This function takes 2 arguments callback and the interval in which it should be called. So let's say the first call to the API went at 1ms and you've set the time as 250ms, until 251ms, a new call wont be made to the API no matter how many times this function was called. You can replace your own function call using this call.
- Related Questions & Answers
- Explain the PowerShell Function.
- Explain 'Not a constructor function' error in JavaScript?
- Explain array_merge() function in PHP.
- Explain array_intersect() function in PHP.
- Explain str_split() function in PHP
- Explain array_map() function in PHP
- Explain substr() function in PHP
- Explain the PowerShell Advanced Function.
- Explain Squeeze Function C language
- Explain Comments in JavaScript
- Explain sets in JavaScript?
- Explain typecasting in Javascript?
- Explain hoisting in JavaScript
- Explain JavaScript Error Object.
- Explain JavaScript text alert