Skip to main content
Participant
December 17, 2017
Answered

30 Second Countdown Timer

  • December 17, 2017
  • 3 replies
  • 1974 views

Hi, I am trying to create a countdown that counts down from 30 seconds for my game.

This is my code:

var secondsLeft = 30;

this.countdown_txt.text = secondsLeft;

var timerId = setInterval(countdown, 1000);

function countdown(){

if(secondsLeft == 0){

     //doStuff

}

else{

     secondsLeft--;

     this.countdown_txt.text = secondsLeft;

     }

}

When I run this I get an error that occurs on line 11: Uncaught TypeError: Cannot set property 'text' of undefined.

I'm not sure why this is the case as I have a similar setup for counting the score when clicking on a movieclip and that works fine. Any help would be appreciated.

This topic has been closed for replies.
Correct answer kglad


"this" is undefined in countdown().  try:


var timerId = setInterval(countdown.bind(this), 1000);

3 replies

Participant
April 8, 2021

Hi,

I have made a clock on a pdf file 

 

But I want to insert a countdown of 120"  in a pdf document (not in a HLML File)

I think I 'll have a text field with the result .... with which properties?

 

an action button start  field with which properties  ?

 

a action button reset with which properties ?

 

Can you help me please?

Thank you by advance

Henri

Participant
December 17, 2017

Yup that's exactly it. Thanks a lot.

kglad
Community Expert
Community Expert
December 17, 2017

you're welcome.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
December 17, 2017


"this" is undefined in countdown().  try:


var timerId = setInterval(countdown.bind(this), 1000);