CancelTokens are handles for the registration of event callbacks.
User can use
CancelTokens to cancel the registration.
Cancels the event callback registration (i.e., the callback won't be invoked any more).
Under the case that
Cancel() is invoked after the time event has finished, nothing would happen.
local totalTimesToMeet = 10
local metCounter = 0
Note that you must declare a variable before registering a callback if you want to use CancelToken in the callback. Otherwise, nil will be returned.
-- Print the log in 2 seconds. Then print the log every second.
cancelToken = YaTimeAPI.ScheduleAtInterval(2, 1, function()
metCounter = metCounter + 1
print("Hey! We have met " .. metCounter .. " times")
-- Cancel the registration of the callback when the execution number reaches totalTimesToMeet and the callback is terminated.
if metCounter == totalTimesToMeet then