From 460363fbef4291d1c99e2a06e25820b543a97c71 Mon Sep 17 00:00:00 2001 From: Travis <thyink@gmail.com> Date: Wed, 9 Dec 2015 12:17:39 -0600 Subject: [PATCH] fix(ngClick): subract processing time of the click action subract the time it takes for the browser to process the click action for preventing the ghost click. long running processes or slower devices where a lot of rendering is happening would get the ghost click every time. --- src/ngTouch/directive/ngClick.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/ngTouch/directive/ngClick.js b/src/ngTouch/directive/ngClick.js index f352c252f443..0b1af1e3fe66 100644 --- a/src/ngTouch/directive/ngClick.js +++ b/src/ngTouch/directive/ngClick.js @@ -57,6 +57,7 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement', var ACTIVE_CLASS_NAME = 'ng-click-active'; var lastPreventedTime; + var processingTime; var touchCoordinates; var lastLabelClickCoordinates; @@ -121,7 +122,7 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement', // Global click handler that prevents the click if it's in a bustable zone and preventGhostClick // was called recently. function onClick(event) { - if (Date.now() - lastPreventedTime > PREVENT_DURATION) { + if (Date.now() - lastPreventedTime - processingTime > PREVENT_DURATION) { return; // Too old. } @@ -197,6 +198,14 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement', checkAllowableRegions(touchCoordinates, x, y); } + function calculateProcessingTime() { + //use the now - last prevented time on a timeout + processingTime = 0; + $timeout(function() { + processingTime = Date.now() - lastPreventedTime; + }, 0); + } + // Actual linking function. return function(scope, element, attr) { var clickHandler = $parse(attr.ngClick), @@ -251,6 +260,7 @@ ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement', if (tapping && diff < TAP_DURATION && dist < MOVE_TOLERANCE) { // Call preventGhostClick so the clickbuster will catch the corresponding click. preventGhostClick(x, y); + calculateProcessingTime(); // Blur the focused element (the button, probably) before firing the callback. // This doesn't work perfectly on Android Chrome, but seems to work elsewhere.