In my Vaadin project when click button then I need:
- Start countdown (10 seconds)
- Replace button's caption by left seconds (10,9,8 and so on)
I try this:
private Button buttonNext, buttonSendSMS;
private void runCountDown() {
logger.info("countDown_start");
buttonSendSMS.setEnabled(false);
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int leftSeconds = 10;
public void run() {
leftSeconds--;
UI.getCurrent().access(new Runnable() {
@Override
public void run() {
logger.info("leftSeconds = " + leftSeconds);
buttonSendSMS.setCaption(leftSeconds + "");
}
});
if (leftSeconds < 0) {
logger.info("countDown_finish");
timer.cancel();
buttonSendSMS.setEnabled(true);
new MessageService(getWzContext().getLang());
buttonSendSMS.setCaption(MessageService.getMessage("resend.sms"));
}
}
}, 0, 1000);
}
When click button here log:
INFO] 12.01.2021 17:35:58.112 [Timer-0] com.myproject.client.ui.wz.SmsCodeStepWz$2$1.run(SmsCodeStepWz.java:202)
leftSeconds = 9
INFO] 12.01.2021 17:35:59.111 [Timer-0] com.myproject.client.ui.wz.SmsCodeStepWz$2$1.run(SmsCodeStepWz.java:202)
leftSeconds = 8
[INFO] 12.01.2021 17:36:00.110 [Timer-0] com.myproject.client.ui.wz.SmsCodeStepWz$2$1.run(SmsCodeStepWz.java:202)
leftSeconds = 7
[INFO] 12.01.2021 17:36:01.110 [Timer-0] com.myproject.client.ui.wz.SmsCodeStepWz$2$1.run(SmsCodeStepWz.java:202)
leftSeconds = 6
[INFO] 12.01.2021 17:36:02.111 [Timer-0] com.myproject.client.ui.wz.SmsCodeStepWz$2$1.run(SmsCodeStepWz.java:202)
leftSeconds = 5
[INFO] 12.01.2021 17:36:03.110 [Timer-0] com.myproject.client.ui.wz.SmsCodeStepWz$2$1.run(SmsCodeStepWz.java:202)
leftSeconds = 4
[INFO] 12.01.2021 17:36:04.110 [Timer-0] com.myproject.client.ui.wz.SmsCodeStepWz$2$1.run(SmsCodeStepWz.java:202)
leftSeconds = 3
But on Web page replace button's caption to 9. Nice.
But after one second NOT REPLACE by 8. And nothing happened. Button's caption is only 9.
As you can see I try to update button's caption on UI thread but it not help.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…