GWT - Handle both double click and single click on the same element

1.4k Views Asked by At

I've a custom widget with a onClick handler. The problem is when user double clicks on the element, a duplicate call is also triggered for the second click. Was trying to avoid it by using onDoubleClick handler, on double click the call does go into this handler but it goes only after executing onClick. So still the duplicate call is happening.

Have seen solutions of having a timer in Javascript to avoid duplicate requests on double. Is it possible to have a similar thing in GWT?

Or is there any other better way of avoiding the duplicate request on double click for elements in GWT?

3

There are 3 best solutions below

0
On BEST ANSWER

How to prevent DoubleSubmit in a GWT application?

The answer given in the above link would help in handling double click for a button.

But if its a custom widget and .setEnabled(false) doesn't work we can have a static boolean variable, condition check if its false then allow and set it to true when clicked once then set it back to false on success. In case of double click the boolean value will be true and the if condition would fail.

0
On

Replace the widget with a 'loading spinner animated image' as long as the widget is doing it stuff. Once ready replace it back with the widget.

Since the image does not contain the clickhandler, the second click will not be received.

0
On

I prevent the double click by taking the current time in milliseconds and check it with the last handling of this method. If it's less than one second between the first and second click, the second one gets prevented.

long lastClick = 0;
if (lastClick < System.currentTimeMillis() - 1000)
{
    // do logic
}
lastClick = System.currentTimeMillis();