Prevent multiple button clicks
Opened this issue · 2 comments
russellhoff commented
Is there any way to prevent the user from clicking several times a button? I mean, I've proven that the user can click several times a button in a short time (0,5 seconds).
Would it be possible to lock every buttons of the wizard just after the user clicks one, and then, when the new step is shown, enable every button?
russellhoff commented
Perhaps it'd be a nice feature to add a modal window (loading) to appear when a button is clicked, and hide it when the new Step is shown.
ldelaprade commented
Created a PaceMaker class for that:
/**
* JetPlaceMaker to use for Action/Function calls regulation
* For example to prevent fast clicking causing multiple calls
*/
public class JetPaceMaker
{
// default minimal time between 2 calls
private long minimalInterval = 1000;
private LocalDateTime lastCallTime = LocalDateTime.now();
public JetPaceMaker() {}
public JetPaceMaker(long interval)
{
minimalInterval=interval;
}
public boolean CanDoActionNow()
{
LocalDateTime now = LocalDateTime.now();
long diffInMilli = java.time.Duration.between(lastCallTime, now).toMillis();
// remember last call time
lastCallTime=now;
// return TRUE when enough time elapsed
if (diffInMilli >= minimalInterval)
return true;
else
Notification.show("Slow down please...");
return false;
}
}
Then, modified Wizard Class code adding a private PaceMaker
// Use JetPaceMaker to prevent fast clicks on Wizard Buttons
private JetPaceMaker fastClickPrevention = new JetPaceMaker();
And using it to control clicks pace (for each button. Here the 'Next' button change)
private void initControlButtons()
{
nextButton = new Button("Next");
nextButton.addClickListener
(
new Button.ClickListener()
{
@Override
public void buttonClick(ClickEvent event)
{
if( fastClickPrevention.CanDoActionNow() )
next();
}
}
);