|
102 | 102 | */ |
103 | 103 | this.tapTimeout = options.tapTimeout || 700; |
104 | 104 |
|
| 105 | + /** |
| 106 | + * The check for whether to track touch event start from event.timeStamp (default) or current time (to handle negative timestamp issue) |
| 107 | + * |
| 108 | + * @type boolean |
| 109 | + */ |
| 110 | + this.isUsingEventTimeStamp = true; |
| 111 | + |
| 112 | + /** |
| 113 | + * The last cick event time stamp |
| 114 | + * |
| 115 | + * @type number |
| 116 | + */ |
| 117 | + this.lastClickTime = 0; |
| 118 | + |
| 119 | + /** |
| 120 | + * The base time to use for comparison, when not tracking touch by event.timeStamp |
| 121 | + * |
| 122 | + * @type number |
| 123 | + */ |
| 124 | + this.nonEventTrackingTimeBase = null; |
| 125 | + |
105 | 126 | if (FastClick.notNeeded(layer)) { |
106 | 127 | return; |
107 | 128 | } |
|
389 | 410 | * @returns {boolean} |
390 | 411 | */ |
391 | 412 | FastClick.prototype.onTouchStart = function(event) { |
392 | | - var targetElement, touch, selection; |
| 413 | + var targetElement, touch, selection, touchTimeStamp; |
| 414 | + |
| 415 | + if (this.isUsingEventTimeStamp && event.timeStamp > -1) { |
| 416 | + touchTimeStamp = event.timeStamp; |
| 417 | + } else { |
| 418 | + if (this.isUsingEventTimeStamp) { |
| 419 | + // Set the base time minus twice the tap delay, to avoid the user seeing a non-response this first time through. |
| 420 | + // This would occur when comparing touchTimeStamp - lastClickTime, which would be roughly 0, and expecting it not to be < this.tapDelay. |
| 421 | + this.nonEventTrackingTimeBase = ((new Date()).getTime() - (this.tapDelay * 2)); |
| 422 | + this.lastClickTime = 0; |
| 423 | + this.isUsingEventTimeStamp = false; |
| 424 | + } |
| 425 | + |
| 426 | + touchTimeStamp = ((new Date()).getTime() - this.nonEventTrackingTimeBase); |
| 427 | + } |
393 | 428 |
|
394 | 429 | // Ignore multiple touches, otherwise pinch-to-zoom is prevented if both fingers are on the FastClick element (issue #111). |
395 | 430 | if (event.targetTouches.length > 1) { |
|
435 | 470 | } |
436 | 471 |
|
437 | 472 | this.trackingClick = true; |
438 | | - this.trackingClickStart = event.timeStamp; |
| 473 | + this.trackingClickStart = touchTimeStamp; |
439 | 474 | this.targetElement = targetElement; |
440 | 475 |
|
441 | 476 | this.touchStartX = touch.pageX; |
442 | 477 | this.touchStartY = touch.pageY; |
443 | 478 |
|
444 | 479 | // Prevent phantom clicks on fast double-tap (issue #36) |
445 | | - if ((event.timeStamp - this.lastClickTime) < this.tapDelay) { |
| 480 | + if ((touchTimeStamp - this.lastClickTime) < this.tapDelay) { |
446 | 481 | event.preventDefault(); |
447 | 482 | } |
448 | 483 |
|
|
519 | 554 | * @returns {boolean} |
520 | 555 | */ |
521 | 556 | FastClick.prototype.onTouchEnd = function(event) { |
522 | | - var forElement, trackingClickStart, targetTagName, scrollParent, touch, targetElement = this.targetElement; |
| 557 | + var forElement, trackingClickStart, targetTagName, scrollParent, touch, touchEndTime, targetElement = this.targetElement; |
| 558 | + |
| 559 | + if (this.isUsingEventTimeStamp) { |
| 560 | + touchEndTime = event.timeStamp; |
| 561 | + } else { |
| 562 | + // iOS 11.3+ and Safari 11.1+ can return negative event.timeStamp values after resuming. |
| 563 | + // https://github.com/ftlabs/fastclick/issues/549 |
| 564 | + touchEndTime = ((new Date()).getTime() - this.nonEventTrackingTimeBase) |
| 565 | + } |
523 | 566 |
|
524 | 567 | if (!this.trackingClick) { |
525 | 568 | return true; |
526 | 569 | } |
527 | 570 |
|
528 | 571 | // Prevent phantom clicks on fast double-tap (issue #36) |
529 | | - if ((event.timeStamp - this.lastClickTime) < this.tapDelay) { |
| 572 | + if ((touchEndTime - this.lastClickTime) < this.tapDelay) { |
530 | 573 | this.cancelNextClick = true; |
531 | 574 | return true; |
532 | 575 | } |
533 | 576 |
|
534 | | - if ((event.timeStamp - this.trackingClickStart) > this.tapTimeout) { |
| 577 | + if ((touchEndTime - this.trackingClickStart) > this.tapTimeout) { |
535 | 578 | return true; |
536 | 579 | } |
537 | 580 |
|
538 | 581 | // Reset to prevent wrong click cancel on input (issue #156). |
539 | 582 | this.cancelNextClick = false; |
540 | 583 |
|
541 | | - this.lastClickTime = event.timeStamp; |
| 584 | + this.lastClickTime = touchEndTime; |
542 | 585 |
|
543 | 586 | trackingClickStart = this.trackingClickStart; |
544 | 587 | this.trackingClick = false; |
|
571 | 614 |
|
572 | 615 | // Case 1: If the touch started a while ago (best guess is 100ms based on tests for issue #36) then focus will be triggered anyway. Return early and unset the target element reference so that the subsequent click will be allowed through. |
573 | 616 | // Case 2: Without this exception for input elements tapped when the document is contained in an iframe, then any inputted text won't be visible even though the value attribute is updated as the user types (issue #37). |
574 | | - if ((event.timeStamp - trackingClickStart) > 100 || (deviceIsIOS && window.top !== window && targetTagName === 'input')) { |
| 617 | + if ((touchEndTime - trackingClickStart) > 100 || (deviceIsIOS && window.top !== window && targetTagName === 'input')) { |
575 | 618 | this.targetElement = null; |
576 | 619 | return false; |
577 | 620 | } |
|
0 commit comments