0

如何禁用此事件侦听器以使其仅触发一次?

document.getElementById('element').ontouchmove = function (e) 
{   
  DISABLE HERE
  do something that should only happen once
};
4

2 回答 2

2
document.getElementById('element').ontouchmove = function (e) {   
  this.ontouchmove = null;

  // Do something here.
};
于 2012-12-13T23:20:07.143 回答
0

首次执行后将布尔值设置为 false,每次在函数运行之前都会对其进行检查:

var doTouchEvent = true;
document.getElementById('element').ontouchmove = function (e) 
{   
   if(doTouchEvent){
      DISABLE HERE
      do something that should only happen once


      doTouchEvent = false;
   }
};
于 2012-12-13T23:19:42.783 回答