Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:41:05+00:00 2026-06-15T23:41:05+00:00

I have a problem with a Timer in a chess i’m doing. It’s working

  • 0

I have a problem with a Timer in a chess i’m doing. It’s working “fine”, except it counts seconds two on two (2:00 > 1:58 > 1:56, etc.. but that’s with a 1-second interval, not 2-second interval)

here’ the code where i define, start, and end the timer:

private void setTime(){
    totalTime=20;
    whiteSec=0;
    whiteMin=totalTime;
    blackSec=0;
    blackMin=totalTime;
    ActionListener taskPerformer = new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              if(whiteActive){
                  if(whiteSec>0) whiteSec-=1;
                  else{
                      whiteMin-=1;
                      whiteSec=60;
                  }
                  if(whiteMin==0 && whiteSec==0) endGame();
                  else GUI.setPlayerTime(whiteMin, whiteSec);
              }else{
                  if(blackSec>0) blackSec-=1;
                  else{
                      blackMin-=1;
                      blackSec=60;
                  }
                  if(blackMin==0 && blackSec==0) endGame();
                  else GUI.setPlayerTime(blackMin, blackSec);
              }
          }
      };
    chessTimer = new Timer(1000, taskPerformer);
}

//start

whiteActive = true;
setTime();
wCastling = true;
bCastling = true;
canEnPassant = false;
GUI.setPlayerTime(whiteMin, whiteSec); //this writes the time in some JLabels.
guiRefresh();
activePiece = null;
chessTimer.start();

//end

private void endGame(){
    GUI.endGame(checkMate);  //shows an endgame JOptionPane
    chessTimer.stop();
}

I’d appreciate any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:41:06+00:00Added an answer on June 15, 2026 at 11:41 pm

    While I do not believe a Timer can be started twice, multiple calls to setTime() would create multiple timers, each of which would independently decrement the fields (until the first is garbage collected, which may or may not happen). If you call the method twice in a row, the two Timer objects would coexist for a while, and it would probably decrement twice per second; calling stop() would stop one of the timers and keep the other around.

    As a debugging step (and a good practice overall), check that you don’t already have a timer before you create a new one:

    /* ... */
    if (chessTimer != null) throw new IllegalStateException("setTime already called");
    chessTimer = new Timer(1000, taskPerformer);
    

    To fix it, track down the duplicate call, or band-aid over it by replacing the IllegalStateException with chessTimer.stop();.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem that I need to create timer but I want to pass
I have a problem that I'm working on for quite some time now. I
I'm doing a hovercard plugin for my site but have a problem with getting
I have a problem with the Timer class in that there is a chance
I have problem with setTimeout. In all major browsers it works fine but not
I have some code that will be accessed from two threads: class Timer{ public:
Basically i have a problem with this timer program I am trying to put
I have a problem with a sheet that when opened for the first time,
Have a problem, I'm about to rebuild the search feature on my site, but
The N-Queens Problem: This problem states that given a chess board of size N

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.