Skip to main content
melfs
Participant
June 2, 2014
Answered

Timer - How to stop it?

  • June 2, 2014
  • 1 reply
  • 318 views

Hello,

I am learning how to make a timer to my game. It is something like "Who wants to be a millionaire?" I have 15 questions. There is timer for each question:60 seconds.... When I click on an answer I go on next frame, but my timer does not stop. What should I add ( and where ) to stop my timer when I clik an answer? My code for the timer is:


timer = 60;

countdown = function(){

_root.timer--;

if(_root.timer<=0){

  gotoAndPlay(20); stop();

}

}

countdownInterval = setInterval(countdown,1000);

This topic has been closed for replies.
Correct answer kglad

use clearInterval:

clearInterval(countdownInterval);

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
June 2, 2014

use clearInterval:

clearInterval(countdownInterval);

melfs
melfsAuthor
Participant
June 2, 2014

Thank you! Where should I add it in my code ? Sorry for the question but this is my first time trying to do a game in Flash.

kglad
Community Expert
Community Expert
June 2, 2014

you should always add it just before setting it.  that prevents a major difficult-diagnose-problem that you have yet to encounter but almost certainly eventually will.

and you should add it when you want to stop the interval.

from my understanding of your setup, you only want that timer to execute once on each frame and you probably want it to stop when the user answers a question.  so, you should use clearInterval when a user answers the question and when the countdown executes.

p.s.  you can uset setTimeout for a timer that only executes, at most, once.  that would eliminate the need to use clearTimerout in countdown, but you would still use it when the user answers a question.