Created
January 3, 2024 16:23
-
-
Save anisur2805/df4527952b140160d96faf5f921e866d to your computer and use it in GitHub Desktop.
JS Debouce example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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