Skip to content

Instantly share code, notes, and snippets.

@Lwdthe1
Last active March 31, 2016 22:28
Show Gist options
  • Save Lwdthe1/1a07d5c329f6cfb2763f to your computer and use it in GitHub Desktop.
Save Lwdthe1/1a07d5c329f6cfb2763f to your computer and use it in GitHub Desktop.
A jQuery function to run a function when an element is long pressed

How to use:

1st: Make sure jQuery is included in your code

2nd:Include the script where in your code after jQuery has been included.

3rd: $('#pressable-element').setLongPress(function(){alert("LONG PRESSED")});

(function($){
$.fn.setLongPress = function(longPress){
var timer;
var doLongPress = function(){
timer = setTimeout(longPress, .35 * 1000);
}
this.on("mousedown", doLongPress).on("mouseup mouseleave",function(){
clearTimeout(timer);
});
}
})(jQuery);
DO WHATEVER YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2016 Lincoln W Daniel <http://lincolnwdaniel.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment