Skip to content

Instantly share code, notes, and snippets.

@happymishra
Created July 1, 2019 04:51
Show Gist options
  • Save happymishra/051583a32d423618c356e6436762c5ce to your computer and use it in GitHub Desktop.
Save happymishra/051583a32d423618c356e6436762c5ce to your computer and use it in GitHub Desktop.
<html>
<body>
<button id="click-btn">Click me!</button>
</body>
<script>
var timerId;
var clickBtn = document.getElementById('click-btn');
var clickFunction = function () {
console.log("No matter how many times you click me," +
"I will be called only 1 time in 1 second!")
}
var throttleFunction = function (func, delay) {
if (timerId) {
return
}
timerId = setTimeout(function () {
func()
timerId = undefined;
}, 1000)
}
clickBtn.addEventListener('click', function () {
throttleFunction(clickFunction, 200)
})
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment