Permalink
Browse files

New feature: repeat

  • Loading branch information...
1 parent d296e4f commit 6ce3834db9c14078b91c3d45a391aecb1e193121 @pixelmatrix committed May 6, 2010
Showing with 31 additions and 19 deletions.
  1. +8 −0 README.textile
  2. +21 −14 jquery.clickHold.js
  3. +2 −5 test.html
View
@@ -40,6 +40,14 @@ clickHold(1000, {touchEvents: false}, function(){
alert("Normal computers only!");
});
+h4. repeat (boolean)
+
+If you want the callback to repeat while you're holding the mouse down (after the initial delay) set this property to true
+
+h4. interval (integer)
+
+This interval is for the repeat. Once the initial delay fires the event the first time, this property changes the repeat delay.
+
h3. callback
The function to run when the clickHold event fires.
View
@@ -31,10 +31,12 @@ options:
touchEvents: (boolean)
Enable this event for Touch devices (iPhone/iPad)
+Dedicated to Josh Jenkins and Mike Sigler
+
Enjoy!
*/
-
+var _t, _r;
(function($) {
$.fn.clickHold = function(ms, options, trigger) {
//map vars correctly
@@ -51,8 +53,6 @@ Enjoy!
// build element specific options
var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
- var t; //our timer, scoped to the element
-
//support keywords
if(typeof(ms) == "string"){
if(ms == "short" || ms == "fast"){
@@ -69,18 +69,18 @@ Enjoy!
$this.unbind(".clickHold");
//call events
$this.bind("mousedown.clickHold",function(){
- $.fn.clickHold.mdown(t, trigger, ms, $this);
+ $.fn.clickHold.mdown(trigger, ms, $this, o);
}).bind("mouseup.clickHold",function(){
- $.fn.clickHold.mup(t);
+ $.fn.clickHold.mup();
});
//call touch events if enabled
if(o.touchEvents){
$this.bind("touchstart.clickHold", function(){
- $.fn.clickHold.mdown(t, trigger, ms, $this);
+ $.fn.clickHold.mdown(trigger, ms, $this, o);
}).bind("touchend.clickHold", function(){
- $.fn.clickHold.mup(t);
+ $.fn.clickHold.mup();
}).bind("touchcancel.clickHold", function(){
- $.fn.clickHold.mup(t);
+ $.fn.clickHold.mup();
});
}
}else{
@@ -89,23 +89,30 @@ Enjoy!
});
};
- $.fn.clickHold.mdown = function(t, trigger, ms, el){
+ $.fn.clickHold.mdown = function(trigger, ms, el, o){
el.unbind("click.clickHold");
- t = setTimeout(function(){
+ _t = setTimeout(function(){
el.bind("click.clickHold", function(){
return false;
});
- trigger();
+ if(o.repeat){
+ _r = setInterval(trigger, o.interval);
+ }else{
+ trigger();
+ }
}, ms);
}
- $.fn.clickHold.mup = function(t){
- clearTimeout(t);
+ $.fn.clickHold.mup = function(){
+ clearTimeout(_t);
+ clearInterval(_r);
}
//
// plugin defaults
//
$.fn.clickHold.defaults = {
- touchEvents: true
+ touchEvents: true,
+ repeat: false,
+ interval: 300
};
})(jQuery);
View
@@ -11,11 +11,8 @@
<script type="text/javascript" charset="utf-8">
$(function(){
$("#weewee").hide();
- $("#holdme").clickHold("normal", function(){
- $("#weewee").fadeIn();
- setTimeout(function(){
- $("#weewee").fadeOut();
- }, 5000);
+ $("#holdme").clickHold("normal", {repeat: true}, function(){
+ alert("boom!");
});
});

0 comments on commit 6ce3834

Please sign in to comment.