How to get the returned value of a function called from settimeout

379 views Asked by At

I have live agent set up and I am trying to set an alert on the agent side when a customer does not reply to his messages after a certain interval of time

Here is how I am trying to achieve (All these are in the js helper,being delegated from the js controller)

  1. Declared A global variable globalvariable1 which keeps track of its changed value

  2. Used an onAgentMessageSend component which fires when an Agent chats

onAgentMessageSend : function(cmp,recordId,content,name,type,timestamp){
    if(type=='Agent')
       {
           var self = this;
           setTimeout(function() {
           var returnedType=self.checkMessageSenderAndResetCounter(cmp);
          }, 5000);           
    
           if(returnedType=='Default Value')
           {alert('Unresponsive Customer');}
           else{
           console.log('customer responded hence reset');
           globalvariable1= 'Default Value'}}
           else{
            console.log('Do nothing');}},
  1. A function to change the global value as and when coming from Agent or Customer
 checkMessageSenderAndResetCounter: function(cmp) {
     
     console.log('the current value of globalvarable'+this.globalvariable1);
     return this.globalvariable1;},
  1. A method which is fired when a customer messages

    onNewMessageFromEndUser : function(cmp,recordId,content,name,type,timestamp){

      this.globalvariable1='Customer';},
    

Summing up, what I am trying to achieve here is once the agent chats, a timer is set for lets say 5 seconds. Within that time frame if a customer replies, the global variable changes and it continues. In case there is no reply from the customer's end, the global variable is still a default value, then it raises an alert indicating that there is an unanswered question.

But the result from the Settimeout is not achievable, could you please help out here for any other way to achieve this, or rectify this itself?

1

There are 1 answers

2
Metabolic On

You need to perform your logic either inside setTimeout or create a function and call it inside timeout.

if(type=='Agent')
   {
       var self = this;
       setTimeout(() => {
       var returnedType=self.checkMessageSenderAndResetCounter(cmp);
       
       // perform rest of your logic inside
       if(returnedType=='Default Value') {
           alert('Unresponsive Customer');
       else{
           console.log('customer responded hence reset');
           globalvariable1= 'Default Value';
        }
       
      }, 5000);           
},