@lipe11
Posted
hi,
I think the timeout worked pretty good. I tried to fast click the button but barely saw it act weirdly.
However, if you want to get rid of the effect, you might want to try something like throttling. I'll leave a reference here. As a first approach, you could try something like this
let waiting = false
buttonWrapper.addEventListener("click", (e) => {
if (waiting) return
waiting = true
getAdvice()
setTimeout(() => {
waiting = false
}, 2000);
})
Marked as helpful
@GregLyons
Posted
@lipe11 Thanks for pointing that out. I've applied throttling in a previous project when making calls to an API, but I didn't know it well enough to realize that I should apply it here as well. The video was helpful, too!
I realize now the issue: I was putting removal for the "--disabled" class on a 2-second timeout, but I was allowing such a timeout to be set on every click of the button, even when it was disabled. So if I clicked on the button rapidly, say:
- once, (when the button is enabled, which disables the button)
- again, one second later (while the button is still disabled)
- again, two seconds later (when it becomes enabled again),
then the timeout from step 2 would enable the button at 3 seconds (only one second after step 3), rather than at 4 seconds (two seconds after step 3, as intended). Your suggestion is precisely the way to avoid this.