# Not understanding the timer manual?

**URL:** <https://forum.defold.com/t/not-understanding-the-timer-manual/72842>\
**Category:** Questions\
**Created:** [March 12, 2023, 9:30am UTC](https://forum.defold.com/t/not-understanding-the-timer-manual/72842 "2023-03-12T09:30:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyion](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/cyion/32/30777_2.png) [@Cyion](https://forum.defold.com/u/Cyion)\
**Post date:** [March 12, 2023, 9:30am UTC](https://forum.defold.com/t/not-understanding-the-timer-manual/72842/1 "2023-03-12T09:30:33Z")

</div>

I’ve messed around with the timer.delay() function as i would like for a subroutine to wait for 3 seconds and then update a variable. The reason i’m doing this is to give my player a small period of invincibility so that the player can only lose 1 life every 3 seconds.

looking at the manual i think i need to use the ![image](https://forum-defold.b-cdn.net/uploads/default/original/3X/4/4/4461e4c5976b2d718e1f59bc60f4e3b5bea3a791.png) function.

would i be correct in saying that this will automatically create a timer without any extra code required.  
Also i understand the delay will be the number of seconds in the delay (for me this will be 3), repeat will be set to false because i only want to run this timer when the subroutine is called, But what is the callback for?

below is how i thought the code should be implimented but it must be incorrect because i’m getting an error becasue delay is set to a nil value?

```lua
local function update_damage(self, handle, time_elapsed)
	self.enemy_damage = false
end

local function player_damage(self, normal, distance)
	self.enemy_contact = true
	msg.post("player#player_hit", "play_sound")
	msg.post("/health#health", "remove_life")
	timer.delay(3, false, update_damage(self, handle, time_elapsed))
end

```

I’ve tried a few different things and looked at online videos but can’t seem to understand how it works. Can anyone tell me how the timer.delay() function works?

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [March 12, 2023, 9:33am UTC](https://forum.defold.com/t/not-understanding-the-timer-manual/72842/2 "2023-03-12T09:33:36Z")

</div>

> [@Cyion](#):
>
> `timer.delay(3, false, update_damage(self, handle, time_elapsed))`

That’s not how callbacks work.  
The `update_damage(self, handle, time_elapsed)` is a _function call_, and will return a value.

An alternative would be to do:

```lua
timer.delay(3, false, function (self, handle, time_elapsed)
        print("hello")
        self.enemy_damage = false
    end)

```

---

<div class="post-metadata">

**Author:** ![Pawel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pawel/32/24623_2.png) [@Pawel](https://forum.defold.com/u/Pawel)\
**Post date:** [March 12, 2023, 9:25pm UTC](https://forum.defold.com/t/not-understanding-the-timer-manual/72842/3 "2023-03-12T21:25:36Z")

</div>

You just used a function call as Mathias said, instead of passing a function 😉 You can pass a variable name holding a function in Lua, as I imagine you tried, so only pass variable name:

```
timer.delay(3, false, update_damage)

```

Instead of using the variable name with () which means you will call it (update\_damage() is a call)

The reason you can just pass variable as a callback is that Lua treats functions as first class citizens - so exactly like this - you can pass variable “containing” a whole function. As this 3rd parameter in ‘timer.delay’ is a callback and expects such function - you suspect it is called inside.

This is so powerful feature of Lua, it’s worth learning 😉 If you are familiar with some other language, e.g. C++ you can check out how lambdas are used as callback functions. Mathias shown an example with anonymous callback (defined in a moment of passing to function call with function() end), here is also a proposition with named callback, similar to yours:

```
local my_callback = function(param1)
    print(param1
end
local some_function_like_timer_delay(param, callback)
     callback(param)
end

some_function_like_timer_delay("Pawel", my_callback)

```

Here a call to ‘some\_functuon\_like\_timer\_delay’ will print “Pawel”, because it calls given callback with given param As first argument.

This is powerful, but also cheeky! 😅

You notice the fu action responsible for calling the callback is eventually passing parameters to the function call. When you check out timer.delay it’s callback is described as a function with parameters ‘self, handle, time\_elapsed’ and its responsibility of ‘timer.delay’ to pass this parameters to the callback call.

You, as a user of ‘timer.delay’ need only to provide an anonymous function (‘function() end’) or a “named function” or in other words a variable that is “containing such function” as a callback - that eventually take those parameters and uses them in their body.
