Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Fix negative event.timeStamp values on iOS 11.3 #550

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions lib/fastclick.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,6 @@
var deviceIsIOS4 = deviceIsIOS && (/OS 4_\d(_\d)?/).test(navigator.userAgent);


/**
* iOS 11.3 has a bug that returns negative event.timeStamp values
*
* @type boolean
*/
var deviceIsIOS11_3 = deviceIsIOS && (/OS 11_3(_\d)?/).test(navigator.userAgent);

/**
* iOS 6.0-7.* requires the target element to be manually derived
*
Expand Down Expand Up @@ -397,16 +390,18 @@
*/
FastClick.prototype.onTouchStart = function(event) {
var targetElement, touch, selection, touchStartTime;

if (deviceIsIOS11_3) {
// Do not use event.timeStamp
// iOS 11.3 returns negative values for event.timeStamp after pausing/resuming a Cordova application
// and supposedly for mobile Safari as well
// iOS (at least 11.3 and 11.4 beta) returns negative values for event.timeStamp after pausing/resuming a Cordova application
// and supposedly for mobile Safari as well
// https://github.com/ftlabs/fastclick/issues/549
if (event.timeStamp < 0) {
touchStartTime = (new Date()).getTime();
this.isTrackingClickStartFromEvent = false;
} else {
touchStartTime = event.timeStamp;
this.isTrackingClickStartFromEvent = true;
}

// Ignore multiple touches, otherwise pinch-to-zoom is prevented if both fingers are on the FastClick element (issue #111).
if (event.targetTouches.length > 1) {
return true;
Expand Down Expand Up @@ -536,14 +531,14 @@
*/
FastClick.prototype.onTouchEnd = function(event) {
var forElement, trackingClickStart, targetTagName, scrollParent, touch, touchEndTime, targetElement = this.targetElement;

if (deviceIsIOS11_3) {
// Do not use event.timeStamp
// iOS 11.3 returns negative values for event.timeStamp after pausing/resuming a Cordova application

if (this.isTrackingClickStartFromEvent) {
touchEndTime = event.timeStamp;
} else {
// iOS (at least 11.3 and 11.4 beta) returns negative values for event.timeStamp after pausing/resuming a Cordova application
// and supposedly for mobile Safari as well
// https://github.com/ftlabs/fastclick/issues/549
touchEndTime = (new Date()).getTime();
} else {
touchEndTime = event.timeStamp;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for event.timeStamp to be positive for the touchstart event but negative for the touchend event? In which case, it would be better to only use (new Date()).getTime() instead of the event.timeStamp.

Otherwise, the comparisons below would cause the touchend to exit early:

 		// Prevent phantom clicks on fast double-tap (issue #36)
 		if ((touchEndTime - this.lastClickTime) < this.tapDelay) {

if (!this.trackingClick) {
Expand Down