# Need a variable that increments by 5 every 5th second and retain the value for the whole of it

Rose Jac

Ranch Hand

Posts: 33

posted 5 years ago

Hi.

I have made a swing application. It uses timer FOR performing certain tasks and it uses the following code get the current second:

Now, if the current second is completely divisible by 5, then a graph is updated.ie. graph gets updated every 5th second The graph should begin at time, t = 0; So, what i have done is initialised a variable 'time' to 0. When the second got from above code is divisible by 5, then graph is updated and time is incremented by 5.

Now, the graph is updated for the whole of the 5th second. Now, the problem i face is that the variable time is getting incemented by 5 twice in the 5th second...so, i am multiple values of x-axis (which corresponds to time) as the variable 'time' represents the x-axis, and hece, a wrong result.

I want the variable time to retain the same value for the whole of 5th second and get incremented by 5 in th enext 5th second.

Any idea?

Thanks!

I have made a swing application. It uses timer FOR performing certain tasks and it uses the following code get the current second:

Now, if the current second is completely divisible by 5, then a graph is updated.ie. graph gets updated every 5th second The graph should begin at time, t = 0; So, what i have done is initialised a variable 'time' to 0. When the second got from above code is divisible by 5, then graph is updated and time is incremented by 5.

Now, the graph is updated for the whole of the 5th second. Now, the problem i face is that the variable time is getting incemented by 5 twice in the 5th second...so, i am multiple values of x-axis (which corresponds to time) as the variable 'time' represents the x-axis, and hece, a wrong result.

I want the variable time to retain the same value for the whole of 5th second and get incremented by 5 in th enext 5th second.

Any idea?

Thanks!

Jim Hoglund

Ranch Hand

Posts: 525

Rose Jac

Ranch Hand

Posts: 33

Campbell Ritchie

Marshal

Posts: 52516

118

Rose Jac

Ranch Hand

Posts: 33

Campbell Ritchie

Marshal

Posts: 52516

118

Stephan van Hulst

Bartender

Posts: 6583

84

Matthew Brown

Bartender

Posts: 4568

9

posted 5 years ago

If you can't do that, and your other Timer is on a rapid time interval, you could try this:

- keep track of the last time the graph was drawn (obtained from

- every step, compare the current time to the last time

- if it's more than 5 seconds, redraw the graph and update the "last time" variable

- keep track of the last time the graph was drawn (obtained from

`System.currentTimeMillis()`)- every step, compare the current time to the last time

- if it's more than 5 seconds, redraw the graph and update the "last time" variable