Reputation: 3089
I have a FlexTable and have a click event associated with it. It happens sometimes that events are trigerred multiple times. Sometimes up to 10 times on a single click. I have checked for multiple event handlers but I have only one. As evident from the case that it sometimes run exactly once per click.
Has anyone have any idea about it why it could be happening.
Here is sample snippet, I tried debugging it and found the click handler is fired up multiple times.
private FlexTable flex = new FlexTable(); //it is a global variable
function A(){
flex.clear();
flex.removeAllRows();
flex.removeAllRows();
flex.setBorderWidth(2);
flex.setCellPadding(2);
flex.setCellSpacing(2);
flex.getRowFormatter().addStyleName(0, "historyTableHeader");
flex.setText(0, 0, "ID");
flex.setText(0, 1, "TIME);
flex.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
//some code which might result into call of function B
}
}
//function A is being called multiple times by say function B
Upvotes: 1
Views: 1445
Reputation: 16060
You already gave an answer to your question:
flex.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
//some code which might result into call of function B
}
}
//function A is being called multiple times by say function B
In this case the ClickHandler exists multiple times. You could change your code to:
private FlexTable flex = new FlexTable(); //it is a global variable
private ClickHandler handler = null;
private void A(){
flex.clear();
//
if(handler == null){
handler = new ClickHandler(){}...
flex.addHandler(handler);
}
Better:
private FlexTable flex = new FlexTable(); //it is a global variable
private HandlerRegistration handler = null;
private void A(){
flex.clear();
//
if(handler != null){
handler.removeHandler();
}
handler = flex.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
//some code which might result into call of function B
}
}
But I would suggest to add the handler directly after the creation of the FlexTable and only once.
Upvotes: 6