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
Jack daniel 16Jack daniel 16 

How to call a function in every 5 second in apex?

I want to call a function in every 5-second lightning component controller. 
Best Answer chosen by Jack daniel 16
Deepali KulshresthaDeepali Kulshrestha
Hi Jack,

Greetings to you!

I have written a function as per your requirement. You can use this code
 
waitingTime: null,

doInit_Helper : function(c, e, h) {
h.getTimeSheet(c, e, h);
},

getTimeSheet : function(c, e, h) {
try{

/*   Add your Code here
                          */



this.waitingTime =setTimeout($A.getCallback(() => this.getTimeSheet(c, e, h)), 50000);
}
catch (ex) {
console.log('Exception: '+ex);
}
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

You can use setTimeout for 5 seconds which will call the same function.

Controller:
doInit: function(component, event, helper){
   helper.autoRefresh(component);
}

Helper:
autoRefresh:  function(component){
   var that = this;

   var action = componet.get('c.getData');
   action.setCallback(this, function(result){
       //add your code
   });

   /* enqueue action in this way  */
   $A.enqueueAction(action);
    window.setTimeout(
        $A.getCallback(function() {
            console.log('Calling');
            that.autoRefresh(component);
        }), 5000
    );
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Alok Singh 140Alok Singh 140
Yes Jack,
As Khan Anas  described you need to write a setTimeout function in your javascript controller to autometically calling after 5 seconds.
Deepali KulshresthaDeepali Kulshrestha
Hi Jack,

Greetings to you!

I have written a function as per your requirement. You can use this code
 
waitingTime: null,

doInit_Helper : function(c, e, h) {
h.getTimeSheet(c, e, h);
},

getTimeSheet : function(c, e, h) {
try{

/*   Add your Code here
                          */



this.waitingTime =setTimeout($A.getCallback(() => this.getTimeSheet(c, e, h)), 50000);
}
catch (ex) {
console.log('Exception: '+ex);
}
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
This was selected as the best answer