Hi All. I'll keep it brief.
I have a button that executes its contents, then goes on a 5 second "cooldown" before it can be clicked again. Part of its contents however is a <<repeat>> macro which basically subtracts 0.1s from the $cooldown variable every 100ms, replacing a text span that indicates how much time is left every 100ms as well.
The problem: I can spam click that button really fast, resulting in multiple <<repeat>> macros going off, and multiplying the subtraction so that it subtracts A LOT faster.
Is there a workaround to the problem, or am I coding this button the wrong way? Is there a more creative way of doing this?
The button is something like:
<<button "This goes on cooldown">>
<<if $cooldown > 0>>Do nothing
<<elseif $cooldown is 0>><<set $cooldown to 5>><<repeat 100ms>><<set $cooldown to $cooldown - 0.1>><<replace "#timer">>Cooldown: <<print $cooldown>>s<</replace>><</repeat>>
<</if>>
Comments
Problems:
Here's an example of a button which prevents activations while on cooldown with an <<if>>: The (ndef _cooldown) bit in the outer <<if>> is simply there to allow the button to be used without needing to initialize the cooldown variable first.
Here's a slightly different example which prevents activations while on cooldown by toggling the button's disabled status:
NOTE: Since the buttons use a story/temporary variable to track the cooldown, each such button on the page simultaneously will require its own variable, unless they're supposed to share a cooldown.
I DID add a stop macro to my game's initial button macro.. forgot to add it for this question. My bad. But I definitely coded the buttons badly.
Thanks a lot, TME. Works like a charm.