2

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.

4

1 回答 1

1

好吧,从你的代码中我看到了额外的}

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

也许这}属于forwhile(或其他)迭代。

于 2013-09-11T12:48:03.773 回答