0

I have a LWC component which captures online and offline events and make's apex call accordingly. I have used window object's online eventListener to capture the online event.

After event is fired a apex insert call is made. But the insert function is called multiple time's and many records are created.

I did referred below link but it didn't helped Lightning Component - AddEventListener firing multiple times

Pseudo code for reference:

    whenOnlineDoThis = () => {
        //Apex insert call
    }

    whenOfflineDoThis = () => {
        // Some UI changes
    }

    connectedCallback() {
        window.addEventListener('online',this.whenOnlineDoThis);
        window.addEventListener('offline',this.whenOfflineDoThis);
    }

    disconnectedCallback(){
        window.removeEventListener('online',this.whenOnlineDoThis);
        window.removeEventListener('offline',this.whenOfflineDoThis);
    }

Note: This is mostly observed when user has used pull to refresh when he was offline. So is it the case that, event listener is getting re-register with every pull to refresh?

1 Answer 1

0

this happens when you invoke the function as you are doing:

window.addEventListener('online', this.functionName)

instead of passing it for it to be triggered when the event actually occurs:

window.addEventListener('online', (e) => {this.functionName(e)});
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .