[問題] removeEventListener 失敗?

看板Ajax作者 (灰)時間14年前 (2011/01/03 17:13), 編輯推噓0(0016)
留言16則, 2人參與, 最新討論串1/3 (看更多)
大家好 目前遇到一點瓶頸 囧 我寫一小段要偵測在行動裝置上手勢的事件 但是最後的removeEventListener 似乎沒成功 我在remove的那個function中 加了一段 alert 發現警告視窗數目會遞增 例如第一次彈一個視窗 第二次就彈兩個 想請問我是哪裡寫錯了嗎 謝謝 P.S. 裝置為iPad , $.shove式一個plugin傳入目前的this document.getElementById(“divID”).addEventListener(‘gesturestart’, $.shove(this.onGestureStart, this), false); onGestureStart: function(e) { e.preventDefault(); document.getElementById("divID").addEventListener('gesturechange', $.shove(this.onGestureChange, this), true); document.getElementById("divID").addEventListener('gestureend', $.shove(this.onGestureEnd, this), true); return false; }, onGestureChange: function(e) { e.preventDefault(); … return false; }, onGestureEnd: function(e) { e.preventDefault(); alert('1'); document.getElementById("divID").removeEventListener('gestureend', $.shove(this.onGestureEnd, this), true); document.getElementById("divID").removeEventListener('gesturechange', $.shove(this.onGestureChange, this), true); document.getElementById("divID").removeEventListener('gesturestart', $.shove(this.onGestureStart, this), false); return false; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.204.7.152 ※ 編輯: klern 來自: 218.166.178.204 (01/03 19:53)

01/03 20:47, , 1F
removeEventListener 移除的是第二個參數的函式名稱
01/03 20:47, 1F

01/03 20:48, , 2F
你得第二個參數是 $.shove(....) 應該是移掉了$.shove
01/03 20:48, 2F

01/03 20:51, , 3F
而不是傳進去的this.onGestureEnd 這個函式
01/03 20:51, 3F

01/03 20:54, , 4F
但是移除僅需填函式名$.shove 是否你的外掛就是一個函式
01/03 20:54, 4F

01/03 20:55, , 5F
另外 把$.shove(this.onGestureEnd, this) 包含參數 這樣
01/03 20:55, 5F

01/03 20:57, , 6F
代表的是執行 $.shove(this.onGestureEnd, this)執行的回傳
01/03 20:57, 6F

01/03 20:58, , 7F
而不是 函式名稱的細節
01/03 20:58, 7F

01/03 21:02, , 8F
sorry,細節->繫結 打錯 對這幾個event不了解 以上僅供參考
01/03 21:02, 8F

01/03 21:56, , 9F
可能要先了解 $.shove(....) 回傳的是什麼
01/03 21:56, 9F
謝謝^^ 附上SHOVE plugin的code $.extend({ shove: function(fn, object) { return function() { return fn.apply(object, arguments); } } }); ※ 編輯: klern 來自: 218.166.182.161 (01/03 22:52)

01/03 23:24, , 10F
shove回傳的是 function() { return this.onGestureend.
01/03 23:24, 10F

01/03 23:27, , 11F
apply(this,e); } 所以並不是直接繫結this.onGestureEnd
01/03 23:27, 11F

01/03 23:29, , 12F
因此 removeEventListener沒有作用到 this.onGestureend
01/03 23:29, 12F

01/03 23:31, , 13F
但是這樣我就很納悶了 為什麼addEventListener可以繫結的到
01/03 23:31, 13F

01/03 23:33, , 14F
你能不能不要用shove 還是你一定非得用
01/03 23:33, 14F

01/03 23:45, , 15F
當初用shvoe來綁住this的用意是 能夠在函式中自由存取
01/03 23:45, 15F

01/03 23:46, , 16F
例this.abc的property 不知道這個寫法是不是有問題@@"
01/03 23:46, 16F
文章代碼(AID): #1D8PCg-m (Ajax)
討論串 (同標題文章)
文章代碼(AID): #1D8PCg-m (Ajax)