Question

I'm using the code below, but when I change CountDownTimer(30000, 1000) to CountDownTimer(30000, 500), the only thing that happens is that the score speed increases, but the timer does not count faster. Why not?

new CountDownTimer(30000, 1000) {

 public void onTick(long millisUntilFinished) {
     score + 10;
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextField.setText("done!");
 }

}.start();

Was it helpful?

Solution

Timer will not count faster when you change its' tick rate to 500ms. Its' time is set to 30000ms which is 30 seconds so it doesn't change.

You changed tick rate but for your TextView you are still using millisUntilFinished / 1000, which still shows 30 to 0 each second.

If you write like below, you will see that it is still 30 seconds, but starts counting from 60 to 0, with each tick 0.5 seconds.

    new CountDownTimer(30000, 500) {

         public void onTick(long millisUntilFinished) {
             score += 10;
             t.setText("seconds remaining: " + millisUntilFinished / 500);
         }

         public void onFinish() {
             t.setText("done!");
         }

        }.start();

If you want it to count faster, you must change total milliseconds part and that is 30000 for your CountDownTimer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top