There's a Lightning Component with a button. Its onClick
attribute points to a client-side controller's function. It invokes helper's function which I would like to start a polling routine to a server-side controller.
Client's code:
initiatePolling : function(cmp, callEvent) {
var myFunction = function() {
console.log('Inside myFunction');
var action = cmp.get("c.myServerSideFunction");
action.setCallback(this, function(response) {
console.log("Inside myServerSideFunction callback");
if (response.getState() === "SUCCESS") {
console.log("Success: " + response.getReturnValue());
} else {
console.log("Error");
}
});
$A.enqueueAction(action);
setTimeout(myFunction, 1000);
}
myFunction();
}
Server's code:
@AuraEnabled
public static String myServerSideFunction() {
return 'Server-side callback';
}
The problem is that after clicking the button, in a console I receive the following messages:
Inside myFunction
Inside myServerSideFunction callback
Success: Server-side callback
Inside myFunction
Inside myFunction
Inside myFunction
etc...
It looks like the server-side callback is received only once. Why does it happen? How can I make it work?