// App.js hoặc component của bạn
import React, { useState, useRef } from "react";
// import FunctionQueue from "./FunctionQueue"; // Import FunctionQueue
import { debounce } from "perfect-debounce";
// const queue = new FunctionQueue();
const App = () => {
const [count, setCount] = useState(0);
const debouncedRef = useRef(null);
// // Function bình thường
// const normalFunction = async () => {
// console.log("Running normal function...");
// // await new Promise((resolve) => setTimeout(resolve, 1000)); // Giả lập thời gian xử lý
// console.log("Normal function done");
// // setCount(count + 1);
// };
// // Function debounce
// const debounceFunction = async () => {
// console.log("Running debounce function...");
// // await new Promise((resolve) => setTimeout(resolve, 2000)); // Giả lập thời gian xử lý debounce
// console.log("Debounce function done");
// };
// Gọi function bình thường và debounce, thêm vào queue
const handleNormalClick = () => {
// queue.addFunctionToQueue(normalFunction);
console.log("Normal function done");
};
const handleDebounceClick = () => {
// queue.addFunctionToQueue(debounceFunction);
debouncedRef.current = debounce(async () => {
setCount(count + 1);
console.log("Debounce function done");
}, 500);
};
return (
<div>
<button onClick={handleNormalClick}>Run Normal Function</button>
<button onClick={handleDebounceClick}>Run Debounce Function</button>
<div>Count: {count}</div>
</div>
);
};
export default App;
Last active
December 24, 2024 15:00
-
-
Save hungdev/4168253a7bcc37dbd696f5925eadfced to your computer and use it in GitHub Desktop.
c
ngobach
commented
Dec 24, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment