T

TechIdea

Ecosystem

← Back to JavaScript Challenges
JavaScriptIntermediate

Implement a Debounce Function

Highly relevant. Tests your understanding of closures, setTimeout, and the 'this' keyword.

Problem Statement

Implement a `debounce` function that takes a function `fn` and a time `t` in milliseconds as input, and returns a debounced version of that function.

Real World Scenario:

When building a search input, you don't want to make an API call for every single keystroke. Instead, you wait until the user stops typing for 500ms before making the request.

Input / Output

Input: fn = () => console.log('Searching...'), t = 500

Output: The console.log will only execute 500ms after the last invocation.

Constraints

  • The function should delay the execution of fn until after t milliseconds have elapsed since the last time it was invoked.
  • It must correctly pass arguments and the 'this' context to fn.

Interactive Playground

Loading...
Console Output
Click "Run Code" to see the output here.

Need a hint?

Growth Newsletter

Get practical AI tools, SEO tips, and growth guides weekly.

Join creators, students, and businesses scaling with TechIdea.