質問

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.

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top