Member-only story
JavaScript Special Subject
Write a Better Debounce Function With Underscore
In front-end development, we often encounter some frequent event triggers, such as:
- resize, scroll
- mousedown, mousemove
- keyup, keydown
……
Here’s a demo to see how these events are triggered frequently.
First, let’s write an index.html
file:
The code for the debounce.js
file is as follows:
var count = 1;
var container = document.getElementById('container');
function getUserAction() {
container.innerHTML = count++;
};
container.onmousemove = getUserAction;
Let’s look at the effect: