# Timer.delay doesn't fire (solved)

**URL:** <https://forum.defold.com/t/timer-delay-doesnt-fire-solved/68978>\
**Category:** Questions\
**Created:** [July 29, 2021, 7:43pm UTC](https://forum.defold.com/t/timer-delay-doesnt-fire-solved/68978 "2021-07-29T19:43:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BinaryMoon](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/binarymoon/32/25543_2.png) [@BinaryMoon](https://forum.defold.com/u/BinaryMoon)\
**Post date:** [July 29, 2021, 7:43pm UTC](https://forum.defold.com/t/timer-delay-doesnt-fire-solved/68978/1 "2021-07-29T19:43:58Z")

</div>

Hi - I am making a shoot 'em up and I want to call a function 2 seconds after my player is destroyed.

I found the `timer.delay` method, and think I’ve added it correctly but it doesn’t seem to run the callback.

I am using the following code:

```lua
function on_message(self, message_id, message, sender)

	-- Collide with enemy
	if message_id == hash("collision_response") and message.other_group == hash("enemies") then

			local pos = go.get_position()
			factory.create( 'fx#explode', pos, nil, { type = hash( "large" ) } )

			timer.delay( 2, false, 
				function( self, handle, time_elapsed )
					msg.post( '/game', 'player-dead' )
				end 
			)

			go.delete()
	end

```

If I run the `msg.post` without the `timer.delay` then it works as I expect. With the `timer.delay` the code runs with no errors but nothing happens when the player is removed.

Have I missed something/ done something wrong?

Thanks!

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [July 29, 2021, 8:07pm UTC](https://forum.defold.com/t/timer-delay-doesnt-fire-solved/68978/2 "2021-07-29T20:07:53Z")

</div>

> [@BinaryMoon](#):
>
> With the `timer.delay` the code runs with no errors but nothing happens when the player is removed.

The problem is that you delete the game object using go.delete() and since the game object is deleted so is the script component that started the timer. When this happens the timer will be cancelled as well.

You should move the go.delete() to inside the timer.delay() callback. If you need to hide things like sprites you can disable them using `msg.post("#mysprite", disable")`

---

<div class="post-metadata">

**Author:** ![BinaryMoon](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/binarymoon/32/25543_2.png) [@BinaryMoon](https://forum.defold.com/u/BinaryMoon)\
**Post date:** [July 29, 2021, 8:16pm UTC](https://forum.defold.com/t/timer-delay-doesnt-fire-solved/68978/3 "2021-07-29T20:16:20Z")

</div>

ah ha - that makes sense. I shall try that instead then. Thanks!

Update: That worked. Thanks again 🙂
