简体   繁体   中英

Timer ending too early?

friends, I have this simple timer, which is suppose to tick every 10ms for the period of 2seconds.

        breathTimer = new CountDownTimer(2000, 10) {

        int currStep = 0;

        public void onTick(long millisUntilFinished) {
            Log.e(String.valueOf(currStep*10),String.valueOf(mSecPerBreath));
            currStep++;
        }

        public void onFinish() {
            Log.e("END: "+String.valueOf(currStep*10),String.valueOf(mSecPerBreath));
        }

    };

    breathTimer.start();

Yet it always stops too early, for example

E/END: 1640: 2000

what do you think might be the problem?

or if I set the timer to 3000, it will end in E/END: 2520: 2000

As per documentation , seems like the onTick execution time is significant compared to the countdown interval (10ms). This could be the reason for the increased period (ie 2520).

The calls to onTick(long) are synchronized to this object so that one call to onTick(long) won't ever occur before the previous callback is complete. This is only relevant when the implementation of onTick(long) takes an amount of time to execute that is significant compared to the countdown interval.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM