Look at the example and follow values in the variables:
local clearPhase = 99 --> clearPhase is 99
print("clearPhase outside", clearPhase) --> "clearPhase outside 99"
local cl = timer.cancel(clearPhase) --> 99 is not a number representing any created timer handle at this point
print("return", cl) --> returns false as in your example
timer.delay(1, false, function(self, clearPhase) --> use name "clearPhase" as reference to value passed to this callback by timer.delay that is invoking it!
print("clearPhase inside 1:", clearPhase) --> "clearPhase inside 1: 0" - 0 is a number representing handle to the timer created here
-- ! Important notice here - by passing the name "clearPhase" here you won't get that variable changed to the handle of the timer, instead a new local reference is created here that holds actual handle value for the scope of the function only!
timer.delay(2, false, function(self) --> do not cover the name "clearPhase" here
print("clearPhase inside 2:", clearPhase) --> "clearPhase inside 2: 99" because you are referencing now to the outer local variable called clearPhase, which was 99
In the first timer you are telling the timer:
- “Hey! Get my callback signature and put your handle in the second argument I called clearPhase”
- “Ok, but you know you defined clearPhase earlier (as 99), right? But your wish is my command, I am creating a variable here with timer’s handle value (0)
If you will use clearPhase here now, you will get timer’s local handle (which is 0), not that outer local (99)”
In the second timer:
- “Hey! Print me the value of clearPhase”
- “Hmm, I don’t see clearPhase in the scope of this function, let me take a look outside…
O! There it is, clearPhase is 99!”
If you are familiar with C++ you might use a knowledge about a difference between passing by value and passing by reference
Now on to your case, as you have a better understanding now:
At this point you are either passing a nil variable here, as you only declared it above with
local clearPhase (you can print it before and note that in the snippet you provided is a typo in Phrase) or you defined it somewhere above, but it is not in the snippet here. In the first case, nil is not a proper timer id to cancel and you should get an error like this:
ERROR:SCRIPT: /main/handler.script:10: bad argument #1 to 'cancel' (number expected, got nil)
If you didn’t get this error you must have defined somewhere above
clearPhrase as some number, which is still not a proper timer handle and thus you have a false returned.
Next, you try to somehow “use” clearPhrase to “pass” it to the function “by reference” (or like a pointer to which you would like to save the handle’s value), which is a callback function invoked, when 5 seconds elapses in your timer.delay call - that’s how I see it and that’s probably how you understand it, right?
As you may suspect, this is not a proper understanding, but don’t worry - look:
The third argument of timer.create is a callback function:
delay” amount of seconds elapses the callback function is called AND parameters are passed into it - self (object), handle (id) and time_elapsed value.
When you are writing its signature like this:
you can then use in the body of this function self as a reference to self object and clearPhrase as handle. That clearPhase here is a different variable than what is declared above as local.
I hope this helps you understand the case!