# Throttle Debounce

```javascript
import { useEffect, useState } from "react";

const throttleFunction = (func, delay) => {
  let lastCall = 0;
  return function () {
    let currentTime = new Date().getTime();
    if (currentTime - lastCall > delay) {
      func();
      lastCall = currentTime;
    }
  };
};

export default function App() {
  const [text, setText] = useState("");
  const handleClick = throttleFunction(() => {
    console.log("clicked");
  }, 1000);

  useEffect(() => {
    let id = setTimeout(() => {
      console.log("Do api call " + text);
    }, 2000);
    return () => {
      clearTimeout(id);
    };
  }, [text]);

  const resizeListener = throttleFunction(() => {
    console.log("resize");
  }, 2000);

  useEffect(() => {
    window.addEventListener("resize", resizeListener);
    return () => {
      window.removeEventListener("resize", resizeListener);
    };
  }, []);

  return (
    <div className="App">
      <input onChange={(e) => setText(e.target.value)} />
      <div>{text}</div>
      <button onClick={handleClick}> Test Throttle </button>
    </div>
  );
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gautamnaik1994.gitbook.io/snippets/web-dev/throttle-debounce-react.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
