2012-10-26 3 views
4

수신기 이벤트 수신기를 제거하지 못했습니다. 아래의 코드가 주어지면 콘솔은 "안녕"을 끝없이 인쇄합니다.postMessage 메시지 이벤트 수신 대기 중지

수신기 :

window.addEventListener("message", function(e){ 
     console.log('hi'); 
     window.removeEventListener("message", function(e){}, false) 
}, false); 

보낸 사람 :

var emiter = setInterval(function(){ 
      console.log('sending message'); 
      window.parent.postMessage(messageData, "*"); 
     }, 1000); 

이 주위에 방법이 있나요?

답변

9

난 당신이 같은 기능에 대한 참조를 가지고있는 리스너를 제거하기 위해 생각이 같은 :이 참조가 없기 때문에

var f = function(e){ 
    console.log('hi'); 
    window.removeEventListener("message", f, false); 
} 
window.addEventListener("message", f); 

그래서 당신의이 작동하지 않는 이유는 그 기능을 청취자로

+0

맞아요 너무 많이 jquery 나는 그것에 대해 깜빡 .. 그 경우에는 어떻게 할 수있는 내 콜백, 이벤트 개체 이외의 매개 변수를 전달합니까? – silkAdmin

+0

나는 너를 믿을 수 없다. 너는 값을 얻을 필요가있다. – ars265

+0

이것은 어떤면에서 도움이 될 수 있지만 여전히 매개 변수를 함수에 전달할 수 있다고 생각하지 않습니다. http://stackoverflow.com/questions/256754/how-to-pass-arguments-to-addeventlistener-listener-function – ars265