Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(ngClick): subract processing time of the click action
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Tr4v1s authored and travis committed Dec 9, 2015
1 parent 25e8c59 commit 460363f
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/ngTouch/directive/ngClick.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
}

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 460363f

Please sign in to comment.