Last active
September 13, 2018 13:00
-
-
Save arfeo/72fb028add3eceaec3d034b3a304aabe to your computer and use it in GitHub Desktop.
[ES6] Long tap event handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Обработчик длительного касания | |
* | |
* @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