@@ -354,7 +354,7 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
354354 private _document : Document ;
355355
356356 /** Timer started at the last `touchstart` event. */
357- private _touchstartTimeout : ReturnType < typeof setTimeout > ;
357+ private _touchstartTimeout : null | ReturnType < typeof setTimeout > = null ;
358358
359359 /** Emits when the component is destroyed. */
360360 private readonly _destroyed = new Subject < void > ( ) ;
@@ -434,7 +434,10 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
434434 ngOnDestroy ( ) {
435435 const nativeElement = this . _elementRef . nativeElement ;
436436
437- clearTimeout ( this . _touchstartTimeout ) ;
437+ // Optimization: Do not call clearTimeout unless there is an active timer.
438+ if ( this . _touchstartTimeout ) {
439+ clearTimeout ( this . _touchstartTimeout ) ;
440+ }
438441
439442 if ( this . _overlayRef ) {
440443 this . _overlayRef . dispose ( ) ;
@@ -802,13 +805,15 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
802805 // Note that it's important that we don't `preventDefault` here,
803806 // because it can prevent click events from firing on the element.
804807 this . _setupPointerExitEventsIfNeeded ( ) ;
805- clearTimeout ( this . _touchstartTimeout ) ;
808+ if ( this . _touchstartTimeout ) {
809+ clearTimeout ( this . _touchstartTimeout ) ;
810+ }
806811
807812 const DEFAULT_LONGPRESS_DELAY = 500 ;
808- this . _touchstartTimeout = setTimeout (
809- ( ) => this . show ( undefined , origin ) ,
810- this . _defaultOptions . touchLongPressShowDelay ?? DEFAULT_LONGPRESS_DELAY ,
811- ) ;
813+ this . _touchstartTimeout = setTimeout ( ( ) => {
814+ this . _touchstartTimeout = null ;
815+ this . show ( undefined , origin ) ;
816+ } , this . _defaultOptions . touchLongPressShowDelay ?? DEFAULT_LONGPRESS_DELAY ) ;
812817 } ,
813818 ] ) ;
814819 }
@@ -839,7 +844,9 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
839844 } else if ( this . touchGestures !== 'off' ) {
840845 this . _disableNativeGesturesIfNecessary ( ) ;
841846 const touchendListener = ( ) => {
842- clearTimeout ( this . _touchstartTimeout ) ;
847+ if ( this . _touchstartTimeout ) {
848+ clearTimeout ( this . _touchstartTimeout ) ;
849+ }
843850 this . hide ( this . _defaultOptions . touchendHideDelay ) ;
844851 } ;
845852
0 commit comments