user984003
user984003

Reputation: 29557

javascript disable event listener after first hit

How do I disable this event listener so that it only fires once?

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

Upvotes: 0

Views: 1509

Answers (2)

rmobis
rmobis

Reputation: 27002

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

  // Do something here.
};

Upvotes: 2

jamis0n
jamis0n

Reputation: 3800

Set a Boolean to false after first execution, which will be checked each time before the function runs:

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


      doTouchEvent = false;
   }
};

Upvotes: 0

Related Questions