Skip to content
Advertisement

My throttle function is not waiting the limit time

I’m learning throttling and I’m having an issue where my throttle method is not waiting the limit time to run.

JavaScript

My output is “Hi” 10 times, but I shouldn’t have 10 times Hi because I have a 6s wait between one call and another.

Advertisement

Answer

throttle takes a callback as a parameter, but you’re invoking display immediately.

JavaScript

is exactly equivalent to

JavaScript

See the problem?

You need a function that invokes display with the argument you want instead:

JavaScript
Advertisement