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
Jugbeer BholaJugbeer Bhola 

Named Function inside Lightning Component

Working in a lightning component and have added a window.addEventListener.  Have been told that is a memory leak and should add a removeEventListener also.  To do that I cannot have an annonymous function.  Could someone please explain how I could create a named function that passes all the necessary objects like component and event and use it inside the LEX component?
doInit : function(component,event,helper) {           
        var action = component.get("c.getRoutine");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if(state == "SUCCESS") {
                var responseWrapper = response.getReturnValue();              
                window.addEventListener('message', function(event) {
                    var message = event.data;  
                    component.set("v.savedMessage",message.data);   
                }, false); 
            }
        });

 
Raj VakatiRaj Vakati
Try like this 
Controller 
 
doInit : function(component,event,helper) {           
        var action = component.get("c.getRoutine");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if(state == "SUCCESS") {
                var responseWrapper = response.getReturnValue(); 
				 var onSuccessCall = function(resuest){
            // BP Status is closed 
            if(onSuccessCall === 'Closed'){
              component.set("v.savedMessage",message.data);   
            }
        } helper.callNamedFunction(component,event,helper ,onSuccess );

		
		
				
            }
        });

Helper 
 
callNamedFunction : function(component,event,helper , onSuccess) {   
		
		
                window.addEventListener('message', function(event) {
                    onSuccess(data) 
                }, false); 
		}

 
Jugbeer BholaJugbeer Bhola
To use removeEventListener(), the event, handler, and useCapture (that last argument of true or false) must be identical to the original.  So the window.addEventListener needs to be something like window.addEventListener('message', SomeFunctionName, false);    The example you gave is still and annonymous function.  No luck yet.