Skip to content

Instantly share code, notes, and snippets.

@asgeo1
Created January 21, 2012 14:35
Show Gist options
  • Select an option

  • Save asgeo1/1652946 to your computer and use it in GitHub Desktop.

Select an option

Save asgeo1/1652946 to your computer and use it in GitHub Desktop.

Revisions

  1. asgeo1 created this gist Jan 21, 2012.
    53 changes: 53 additions & 0 deletions jquery.doubletap.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    //based on blog post that I saw here: http://www.sanraul.com/2010/08/01/implementing-doubletap-on-iphones-and-ipads/
    (function($){
    $.fn.doubletap = function(fn) {
    return fn ? this.bind('doubletap', fn) : this.trigger('doubletap');
    };

    $.attrFn.doubletap = true;

    $.event.special.doubletap = {
    setup: function(data, namespaces){
    $(this).bind('touchend', $.event.special.doubletap.handler);
    },

    teardown: function(namespaces){
    $(this).unbind('touchend', $.event.special.doubletap.handler);
    },

    handler: function(event){
    var action;

    clearTimeout(action);

    var now = new Date().getTime();
    //the first time this will make delta a negative number
    var lastTouch = $(this).data('lastTouch') || now + 1;
    var delta = now - lastTouch;
    var delay = delay == null? 500 : delay;

    if(delta < delay && delta > 0){
    // After we detct a doubletap, start over
    $(this).data('lastTouch', null);

    // set event type to 'doubletap'
    event.type = 'doubletap';

    // let jQuery handle the triggering of "doubletap" event handlers
    $.event.handle.apply(this, arguments);
    }else{
    $(this).data('lastTouch', now);

    action = setTimeout(function(evt){
    // set event type to 'doubletap'
    event.type = 'tap';

    // let jQuery handle the triggering of "doubletap" event handlers
    $.event.handle.apply(this, arguments);

    clearTimeout(action); // clear the timeout
    }, delay, [event]);
    }
    }
    };
    })(jQuery);