function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dave CraigmileDave Craigmile 

How to Stop A Timer in a LWC Component?

I have an LWC component that starts a timer (see timer code below).

This works just fine but the LWC is on a record page.  When I navigate to several records sequentially the timers multiply like rabbits.  One per visited record even though the component is visually "gone" between navigations from the list view to the record page.

How can I stop the timer embedded in the LWC when a record page is refreshed or navigated away from? 

In other words, what component event hook can I use from within the LWC itself to stop the timer?

Thanks!


startTimer() {
        let interval = this._loadLock ? (new Date(this._loadLock.Expiration__c)).getTime() - new Date().getTime() : 60000;
        let elem = this.template.querySelector('span.countdown');
        let refreshMe = this._wiredValue;
        let timer = window.setInterval(
            
            function() {
                
                let minutes = Math.floor((interval % (1000 * 60 * 60)) / (1000 * 60));
                let seconds = Math.floor((interval % (1000 * 60)) / 1000);
                elem.innerHTML = minutes + "m " + seconds + "s "; 
                if(interval <= 0) {
                    interval = 60000;
                    refreshApex(refreshMe);
                }                
                else if(interval != 60000 && seconds == 0) {
                    refreshApex(refreshMe);
                }                
                interval = interval - 1000;}, 
            1000);
        this._timerPointer = timer;
    }
 

Danish HodaDanish Hoda
Hi Dave,
May I know how are you calling this method startTimer()?
Dave CraigmileDave Craigmile
Sure.  I call it from an @wire funtion so it starts right away and occasionally is stopped and restarted:

@wire(getLoadLock, {loadId: '$recordId'})
    wiredLoadLock(result) {
        if(result) {
            this._wiredValue = result;
            let data = result.data;
            let error = result.error;
            if (data) {            
                if(data.IsOK) {
                    this._loadLock = data.Results;
                    this.setBackground();                
                    if(this._timerPointer) {
                        window.clearInterval(this._timerPointer);
                    }
                    this.startTimer();
                }
                else {
                    LightningUtil.logError(this, data.Error);
                }
                LightningUtil.displayMessages(this, data.Messages);
            } 
            else if (error) {
                LightningUtil.logError(this, error);
            }
        }
    }
Danish HodaDanish Hoda
Hi Dave,
Please try by adding below code in your html file:
connectedCallBack(){
 location.reload();
}

 
Dave CraigmileDave Craigmile
Danish - Thanks for trying to help!   

Did nothing to stop the rogue timers.  I think the key is finding an event I can handle from the component itself when navigation occurs to a list view or another record.....