Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do I make a simple timer in java?

I just want a method to initiate every second, how would I do that?

Update:

I tried that and a bunch of errors just came up, I found one in my java book but I can't access any of my variables within it. Here is what I have:

int interval = 1000;

class MyListener implements ActionListener{

public void actionPerformed(ActionEvent event){

// in here I want to update all of my data from outside for example the interval. I tried

interval += 100;

}

}

MyListener listener = new MyListener();

Timer t = new Timer(interval, listener);

t.start();

Update 2:

That just comes up with another error, what am I doing wrong?

2 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    Here you go, hope this helps!

    var mins,secs,TimerRunning,TimerID;

    TimerRunning=false;

    function Init() //call the Init function when u need to start the timer

    {

    mins=15;

    secs=0;

    StopTimer();

    StartTimer();

    }

    function StopTimer()

    {

    if(TimerRunning)

    clearTimeout(TimerID);

    TimerRunning=false;

    }

    function StartTimer()

    {

    TimerRunning=true;

    window.status="Time Remaining "+Pad(mins)+":"+Pad(secs);

    TimerID=self.setTimeout("StartTimer()",1000);

    Check();

    if(mins==0 && secs==0)

    StopTimer();

    if(secs==0)

    {

    mins--;

    secs=60;

    }

    secs--;

    }

    function Check()

    {

    if(mins==5 && secs==0)

    alert("You have only five minutes remaining");

    else if(mins==0 && secs==0)

    {

    alert("Your alloted time is over.");

    }

    }

    function Pad(number) //pads the mins/secs with a 0 if its less than 10

    {

    if(number<10)

    number=0+""+number;

    return number;

    }

  • 1 decade ago

    Well, to make a simple timer, you just need to type something like this:

    onClipEvent(load){

    time=number of frames you want the timer to last

    onClipEvent(enterFrame){

    if(time<=0){

    //here you insert what you want to happen when the countdown ends

    }else{

    time--

    }

Still have questions? Get your answers by asking now.