Here is one approach you might take to implement a debounce function.
The first argument is the function you’d like to debounce. The second argument represents the amount of time between function calls. If the third argument is a truthy value, the leading edge will be used. The following debounce function uses the trailing edge by default.