Countdown timer starts from a second after it is supposed to

后端 未结 2 1621
陌清茗
陌清茗 2020-12-22 11:30

I have a Countdowntimer object. Basically the timer is supposed to countdown from 10 to 1. What it does is starts the timer from 9 and it ends on 1 but it stays on 1 for abo

相关标签:
2条回答
  • 2020-12-22 11:42

    It seems that the ticks don't come in at perfectly precise intervals as maybe expected. In your case, the value of millisUntilFinished in consecutive onTick callbacks isn't guaranteed to be 10000, 9000, ..., 0.

    The first tick doesn't have 9 seconds (or 9000ms) remaining, it has 9964ms. So, only 36ms has passed from the time you called start() on the CountDownTimer to the time of this first tick callback. Chris' comment on his answer that the "first callback happens after 1000ms" seems to be incorrect (I'd make a comment on his answer, but I don't have the rep!). You're seeing 9 in your text view and log statement because of the classic integer division problem. You're not seeing the additional 0.964 seconds because the result of the division is of type long, which does not have a fractional part. Try using 1000.0 as your divisor in the log statement and remove the Long.toString() and see what value is shown.

    In regards to the hanging towards the end of the countdown, see the following: https://stackoverflow.com/a/12283400/1244019

    Depending on how precise you want your timer, perhaps a solution to your problem would be to keep a counter within your CountDownTimer, updating your TextView and decrementing the counter each time onTick is called. See the following:

    new CountDownTimer(11500, 1000) {
        int i = 10;
        @Override
        public void onTick(long millisUntilFinished) {
            Log.d("timer","Time left: " + i);
            // Update TextView
            i--;
        }
    
        @Override
        public void onFinish() { }
    }.start();
    
    0 讨论(0)
  • 2020-12-22 11:42

    The reason is because the time interval isn't fine enough and callback isn't at exactly quantas of 10,000

    This code in Kotlin is presenting the remaining time punctuately:

    private fun startTimer()
    {
        Log.d(TAG, ":startTimer: timeString = '$timeString'")
    
        object : CountDownTimer(TASK_SWITCH_TIMER, 250)
        {
            override fun onTick(millisUntilFinished: Long)
            {
                val secondsUntilFinished : Long = 
                Math.ceil(millisUntilFinished.toDouble()/1000).toLong()
                val timeString = "${TimeUnit.SECONDS.toMinutes(secondsUntilFinished)}:" +
                        "%02d".format(TimeUnit.SECONDS.toSeconds(secondsUntilFinished))
                Log.d(TAG, ":startTimer::CountDownTimer:millisUntilFinished = $ttlseconds")
                Log.d(TAG, ":startTimer::CountDownTimer:millisUntilFinished = $millisUntilFinished")
            }
    
            @SuppressLint("SetTextI18n")
            override fun onFinish()
            {
                timerTxtVw.text = "0:00"
                gameStartEndVisibility(true)
            }
        }.start()
    }
    
    0 讨论(0)
提交回复
热议问题