You can use Timer
to execute a function after a certain time interval. Here is an example of how to set a 3-second cooldown on a function in Swift:
main.swift629 chars29 lines
In this example, CooldownManager
class manages the cooldown state and myFunction
has a cooldown of 3 seconds. When myFunction
is called, it checks if it's on cooldown. If it is, the function exits early. Otherwise, it prints a message and starts a 3-second cooldown by calling startCoolDown
method. When the cooldown ends after 3 seconds, endCoolDown
method is called and the cooldown is reset.
Note: This code assumes you want to prevent the function from executing again until the cooldown has expired. If not, adjust the code accordingly.
gistlibby LogSnag