Paste event firing many times

1.8k views Asked by At

This is my code for Chrome to handle paste event:

window.addEventListener("paste",processEvent);
function processEvent(e) {      
       console.log("paste event!");
}

This code works fine except that the event is fired many times even if I press the CTRL+V command just once. What could be the reason? And how can I prevent this from happening as its very important that the handler to fire just once per press of the paste command.

Update:

I logged to the console and here is what I mean:

paste event!
clipboard from event
item found: 0 kind=file type=image/png
found image!
paste event!
clipboard from event
item found: 0 kind=file type=image/png
found image!
paste event!
clipboard from event
item found: 0 kind=file type=image/png
found image!

Notice how the same event is fired 3 times.

Update 2:

This code is actually a GWT code wrapped around JSNI:

public native void pasteEventHandler()/*-{
    window.addEventListener("paste",processEvent);
    function processEvent(e) {      
           console.log("paste event!");
    }
}-*/;

And is called during @PostConstruct of the app:

@PostConstruct
public void setup() {
     pasteEventHandler();
}

When the paste event occurs the "page" with transition to another page (from #Page1 to #Page2. When the page transition back to #Page1, the setup() method is fired up.

1

There are 1 answers

1
Snake Eyes On BEST ANSWER

Well, from your code I see additional }

window.addEventListener("paste",processEvent);
    function processEvent(e) {      
       console.log("paste event!");
    }
}  // what's that ?

Maybe that } belongs to a for or while (or whatever) iteration.