TimerService

Provides a timer function that can call a function after a certain period of time or call it repeatedly.

# Methods

[{"index":"void ClearTimer(integer id)","0":"Destroys the timer."}]

[{"index":"integer SetTimer(IScriptable scriptable, func callback, float intervalSeconds, boolean isRepeat, float startDelaySeconds = 0)","0":"Sets a timer that calls the function passed to callback at a specific moment. The scriptable passes the owner of the action to reserve. The action's owner can be logic or component. It will be called for the first time after intervalSeconds if isRepeat is false. It will be called for the frist time after startDelaySeconds if isRepeat is true, and it will be called repeatedly every intervalSeconds. It will retrieve the timer's id if the reservation is successful; if not, it will return 0."}]

[{"index":"integer SetTimerOnce(func callback, float delaySeconds)","0":"Calls the function passed to callback after delaySeconds. Returns the timer's id if the reservation is successful; if not, returns 0."}]

[{"index":"integer SetTimerRepeat(func callback, float intervalSeconds, float startDelaySeconds = 0)","0":"Repeatedly calls the function passed to callback after startDelaySeconds, once every intervalSeconds. Returns the timer's Id if the reservation is successful; if not, returns 0."}]

##### inherited from Service:

[{"index":"EventHandlerBase ConnectEvent(string key, IScriptFunction eventHandler)","0":"Links events. When a key-type event occurs in this service, a handler is notified. The handler receives event parameters."}]

[{"index":"EventHandlerBase ConnectEvent(Type eventType, IScriptFunction eventHandler)","0":"Links events. This service notifies a handler when an eventType event occurs. The handler receives event parameters."}]

[{"index":"boolean DisconnectEvent(string key, EventHandlerBase eventHandler)","0":"Disconnects the event with the key-type event and the handler."}]

[{"index":"boolean DisconnectEvent(Type eventType, EventHandlerBase eventHandler)","0":"Disconnects the eventType event from its handler."}]

[{"index":"void SendEvent(EventType sendEvent)","0":"Generates an event."}]

# Examples

The following is an example of implementing a clock's second hand by rotating an Entity. Sets the function that rotates the Entity to repeat the execution every second.
    
    
    @Component
    script ZAxisRotator extends Component
        property integer TimerId = 0
    
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
        local repeatFunc = function()
        	local transform = self.Entity.TransformComponent
        	local zRot = transform.ZRotation
    
        	transform.ZRotation = zRot - (360.0 / 60.0)
        end
    
        self.TimerId = _TimerService:SetTimerRepeat(repeatFunc,1.0)
        if self.TimerId == 0 then
        	log_error("Timer reservation failed.")
        end
        end
    
        @ExecSpace("ServerOnly")
        method void OnEndPlay()
        if self.TimerId > 0 then
        	_TimerService:ClearTimer(self.TimerId)
        end
        end
    end
    

# SeeAlso

  * TransformComponent
  * [TimerService to Reserve and Execute Actions](/docs?postId=47)