Skip to content

Instantly share code, notes, and snippets.

@arfeo
Last active September 13, 2018 13:00
Show Gist options
  • Save arfeo/72fb028add3eceaec3d034b3a304aabe to your computer and use it in GitHub Desktop.
Save arfeo/72fb028add3eceaec3d034b3a304aabe to your computer and use it in GitHub Desktop.
[ES6] Long tap event handler
/**
*
* Обработчик длительного касания
*
* @param {Function} action Вызываемая функция
* @param {Integer} duration Длительность касания в мс (1000 по умолчанию)
*
* ---
*
* Пример использования:
*
* document.getElementById('el').longTap(() => {
* console.log("It's a long tap!");
* }, 2000);
*
*/
Object.prototype.longTap = function(action, duration = 1000) {
let touchmoved;
let timer;
this.addEventListener('touchend', () => {
if(touchmoved !== true) {
if(timer) {
clearTimeout(timer);
}
touchmoved = false;
}
});
this.addEventListener('touchmove', () => {
touchmoved = true;
if(timer) {
clearTimeout(timer);
}
});
this.addEventListener('touchstart', () => {
touchmoved = false;
timer = setTimeout(action, duration);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment