Created
July 1, 2019 04:51
-
-
Save happymishra/051583a32d423618c356e6436762c5ce to your computer and use it in GitHub Desktop.
This file contains 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
<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