Skip to content

Instantly share code, notes, and snippets.

@anisur2805
Created January 3, 2024 16:23
Show Gist options
  • Save anisur2805/df4527952b140160d96faf5f921e866d to your computer and use it in GitHub Desktop.
Save anisur2805/df4527952b140160d96faf5f921e866d to your computer and use it in GitHub Desktop.
JS Debouce example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" onkeyup="betterSearch()" id="name">
<script type="text/javascript">
const getData = () => {
console.log( 'Fetching data' )
}
const debounce = function(fn, d) {
let timer;
return function(){
let context = this,
args = arguments;
clearInterval(timer)
timer = setTimeout(() => {
fn.apply(context, args)
}, d);
}
}
const betterSearch = debounce(getData, 300);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment