Wow you have been viewing my site since 20 seconds!
+
Sep 7, 2024
function myDebounce(cb, timeout) {
let timer = null;
return function (...args) { // Capture arguments
const context = this; // Preserve the calling context
if(timer){
clearTimeout(timer);
}
timer = setTimeout(function() {
cb.apply(context, args); // Pass arguments and context to the callback
}, timeout);
}
}
function log(message) {
console.log(message);
}
let debouncedLog = myDebounce(log, 3000);
for(let i = 0 ; i < 10 ; i++){
debouncedLog("Hello"); // Will log "Hello" after 3 seconds only once
}
Check my latest Blog Post