It schedules a call to a function after a period of time has passed.
Timer(delay: double, function: FunctionReference, repeat: Bool)
It creates a timer
instance that programmes the argument function execution after a determined amount of seconds which is equivalent to the delay
argument. The repeat
argument signals if the timer is repetitive, i.e., if the argument function calling should be repeated or not after the first call. The function must be referenced with the following syntax: @function(moduleName.functionName)
and it should have a signature fulfilling 2 conditions: to return void
and to receive only one Timer argument.
Arguments | |
---|---|
delay | Double: delay seconds (it must be a positive value). |
function | FunctionReference: reference to a function in a module. |
repeat | Bool: signals if the timer is repetitive. |
Timer(delay: double, function: FunctionReference)
It creates a timer
instance that programmes the argument function execution after a determined amount of seconds which is equivalent to the delay
argument. This constructor creates a non-repetitive timer instance, that is to say, the argument function calling happens only once. The function must be referenced with the following syntax: @function(moduleName.functionName)
and it should have a signature fulfilling 2 conditions: to return void
and to receive only one timer argument.
Arguments | |
---|---|
delay | Double: delay seconds (it must be a positive value). |
function | FunctionReference: reference to a function in a module. |
Constructor exceptions | |
---|---|
ModuleNotFound – Module ‘moduleName’ not found in function argument of ‘Timer()’ call. | The referenced module cannot be found in the ‘function’ argument of the constructor. |
FunctionNotFound – Function ‘functionName’ not found in module ‘moduleName’ in function argument of ‘Timer()’ call. | The function cannot be found in the ‘moduleName’ module referenced in the ‘function’ argument of the constructor. |
WrongFunctionSignature – Wrong signature in function argument ‘module.functionName()’ of ‘Timer()’ call. Expected signature: ‘void function(Timer aTimer)’. | The signature of the ‘function’ argument function does not coincide with an expected signature of the ‘void function (Timer aTimer)’ type. |
WrongDelayInTimer – Delay argument cannot be negative or zero value. | The ‘delay’ argument cannot be negative or zero value. |
PUBLIC METHODS
void fire()
It triggers the calling on the function
argument, by which the timer instance was built. In case there are repetitive timers, they will not be deprogrammed. On the contrary, non-repetitive timers are deprogrammed even if the specified time in the delay
argument has not been completed.
void start()
It schedules the execution of the argument function by which the instance was built. This will be called when the time set by the delay
constructor argument is completed.
In case the repeat
argument is false, the timer programming is cancelled after the function calling. A second call to the start method can reboot the timer again.
It should be pointed out that the delay
time can be interrupted in case the application moves to the background in response to an [event in the life cycle of an application] (/matr-doc/es/language/basic#clausulas).
void stop()
It stops the timer in case the latter has already been initiated (i.e. programmed) or does not produce any effect on the contrary.
Example:
Application {
DataListExperience mainExp
Timer aTimer
OnInit {
mainExp = DataListExperience()
broker.ui.push("DataListExperience", mainExp)
}
}
Experience DataListExperience {
Array<Data> data as List
OnCreate {
//It schedules a timer that will update the data list on screen every 60 seconds
aTimer = Timer(delay:60, function: @function(DataRepository.fetchDataJob), repeat: true)
aTimer.start()
}
OnDestroy {
aTimer.stop()
}
}
Module DataRepository {
void fetchDataJob(Timer timer) {
mainExp.data = service.loadData.call()
}
}