Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to limit the inputs on a button's execution. Or maybe an alternative solution?

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

  • Please use the code tag when posting code or markup—it's C on the editor bar.


    Problems:
    • You're not stopping the <<repeat>> when the cooldown is over, so each time you activate the button you're creating yet another instance. That's bad, BTW.
    • Unless you have need for the cooldown variable outside of the button, use a temporary variable.
    • You're not gating the initial assignment to the cooldown variable, so each activation resets it to the max.
    • You do not need a useless <<if>> clause simply so you can have an <<elseif>>. Write the <<if>> condition properly and you don't need the <<elseif>>.
    • Do not use numbers with a fractional part for a counter as they're a floating point type. Floating point numbers are approximations, trading precision for range—i.e. they are not exact. While you could solve the issue with clamping, it would be better avoid it altogether.

    Here's an example of a button which prevents activations while on cooldown with an <<if>>:
    <<button "This goes on cooldown">>
    	<<if (ndef _cooldown) or _cooldown is 0>>
    		<<set _cooldown to 50>>
    		<<repeat 100ms>>
    			<<replace "#timer">>Cooldown: <<print Number(_cooldown + 'e-1').toFixed(1)>>s<</replace>>
    			<<if _cooldown gt 0>>
    				<<set _cooldown to _cooldown - 1>>
    			<<else>>
    				<<stop>>
    			<</if>>
    		<</repeat>>
    	<</if>> 
    <</button>>
    
    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:
    <span id="btn-42"><<button "This goes on cooldown too">>
    	<<script>>$('#btn-42 button').prop('disabled', true)<</script>>
    	<<set _cooldown to 50>>
    	<<repeat 100ms>>
    		<<replace "#timer">>Cooldown: <<print Number(_cooldown + 'e-1').toFixed(1)>>s<</replace>>
    		<<if _cooldown gt 0>>
    			<<set _cooldown to _cooldown - 1>>
    		<<else>>
    			<<script>>$('#btn-42 button').prop('disabled', false)<</script>>
    			<<stop>>
    		<</if>>
    	<</repeat>>
    <</button>></span>
    


    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.
  • Sorry, my question wasn't very clear, and sorry about not knowing about the the code tag xD

    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.
Sign In or Register to comment.