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
ankethaanketha 

setInterval in lightning doesn't work

Hi All,

I want to refresh a lightning component for every second, so I'm trying the below code which will call my apex class which is retrieved in the component for evry second. But somehow it doesn't work and i get an error.

window.setInterval(
   $A.getCallback(function() {                      helper.DisplayBusinessHoursForOpenCases(component,helper);
  }), 1000 ); 

I get an error that Uncaught Error in $A.getCallback() [Cannot read property 'DisplayBusinessHoursForOpenCases' of undefined].
I have tried using this.DisplayBusinessHoursForOpenCases, but that doesn't work either.

But the below links show the same example, but this doesn't work in my case.

http://www.infallibletechie.com/2018/04/agetcallback-usage-for-polling-in.html
https://www.jitendrazaa.com/blog/salesforce/calling-apex-method-at-regular-interval-from-lightning-component/

Can anyone please help.

Regards,
Anketha
Alex Bondarenko 1Alex Bondarenko 1
Hello Anketha,
There is not enough this code example to help you, could you post your Helper file code here?
ankethaanketha
The helper code is as below:

({    
    waitingTimeId: null,
    CalculateBusinessHours : function(component) {
        
        var action = component.get("c.getBusinessHours");
        //var self = this;
        action.setParams({IDField: component.get("v.recordId"), ClosedField:"IsClosed"});
        action.setCallback(this, function(response) {
            
            var state = response.getState();
            if(state === "SUCCESS") {   
               var StoreResponse = response.getReturnValue();
                //If the case is Open(IsClosed=false)
                    if(StoreResponse==false){
                        /* window.setInterval(
                            $A.getCallback(function() { 
                            helper.DisplayBusinessHoursForOpenCases(component,helper);
                            }), 1000
                        ); */
                        this.DisplayBusinessHoursForOpenCases(component);
                    }
                    else{
                        this.DisplayBusinessHoursForClosedCases(component);
                    }
            }
            else if (state === "ERROR") {
                alert('Error: '+response.getError());
            }
        });
        $A.enqueueAction(action);
    },
    DisplayBusinessHoursForOpenCases : function(component){
        var action = component.get("c.getBusinessHoursOpen");
        
        action.setParams({IDField: component.get("v.recordId"), "ClosedField":"IsClosed" , "ClosedDateField": "ClosedDate" });
        action.setCallback(this, function(response) {
            
            var state = response.getState();
            if(state === "SUCCESS") {   
                var resultINmsec = response.getReturnValue();
               this.waitingTimeId = setInterval(function() {
                var days = Math.floor((resultINmsec % (1000 * 60 * 60 * 24 * 365)) / (1000 * 60 * 60 * 24));                   
                var hours = Math.floor((resultINmsec % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                var mins = Math.floor((resultINmsec % (1000 * 60 * 60)) / (1000 * 60));
                var secs = Math.floor((resultINmsec % (1000 * 60)) / 1000);
                //var msecs = Math.floor((resultINmsec % (1000)));
                resultINmsec += 1000;
                   this.TimeDiff = days + ':' + hours + ':' + mins + ':' + secs + ':' + 'totalMillisec - ' +  resultINmsec ;
                component.set("v.bh",this.TimeDiff);
                }, 1000);
            }
            else if (state === "ERROR") {
                alert('Error: '+response.getError());
            }
        });
        $A.enqueueAction(action);
        
    },
    
    DisplayBusinessHoursForClosedCases : function(component){
        var action = component.get("c.getBusinessHoursClosed");
        
        action.setParams({IDField: component.get("v.recordId"), "ClosedField":"IsClosed" , "ClosedDateField": "ClosedDate" }); debugger;
       
        action.setCallback(this, function(response) {
            
            var state = response.getState();
            if(state === "SUCCESS") {   
                var resultINmsec = response.getReturnValue();
                var days = Math.floor((resultINmsec % (1000 * 60 * 60 * 24 * 365)) / (1000 * 60 * 60 * 24));                   
                var hours = Math.floor((resultINmsec % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                var mins = Math.floor((resultINmsec % (1000 * 60 * 60)) / (1000 * 60));
                var secs = Math.floor((resultINmsec % (1000 * 60)) / 1000);
                var msecs = Math.floor((resultINmsec % (1000)));

                this.TimeDiff = days + ':' + hours + ':' + mins + ':' + secs + ':' + msecs;
                component.set("v.bh",this.TimeDiff);
            }
            else if (state === "ERROR") {
                alert('Error: '+response.getError());
            }
        });
        $A.enqueueAction(action);
    }

})

SetInterval method works in the days, hours,mins, secs calculation, but doesn't work when I try to call the helper function inside setInterval method(these lines are commented here).

Regards,
Anketha