Skip to content

CancelToken

CancelTokens are handles for the registration of time event callbacks. User can use CancelTokens to cancel the registration.

Functions

void Cancel()

Cancels the time 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.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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.
local cancelToken
-- 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
        cancelToken:Cancel()
    end
end)