Code-based Actions: Combine functions in Javascript

JavaScript can be used for several tasks such as detecting if the user is on a Desktop or Mobile device or checking if the user is on the Homepage or a Specific Page. In this example, we will combine these two functions. We will trigger a specific event after 5 seconds using the following steps:

  1. Create functions.
  2. Combine functions.
  3. Add delayed opening & trigger even.

Create functions

Use the following code to create functions for the above usecase:

<script>
 window.desktopcheck = function() {
     var check = false;
     if(window.innerWidth>768){
         check=true;
     }
     return check;
   }

 window.homepagecheck = function() {  
     var check = false;  
     if(document.location.pathname === "/"){  
         check=true;  
     }  
     return check;  
   }  
</script

Read more about:

Combine functions

To combine functions, both functions need to return true.

<script>

 if(window.desktopcheck() && window.homepagecheck()){  
    // Add code  
  }

</script>

Add delayed opening and start event

Now let's add the delayed opening and trigger an event using the following code example:

<script>

// Delayed Opening  
setTimeout(function() {  
   \_\_flowai_webclient_app.open()  
  }, 5000) // 5 secs

// Trigger Event  
  window.\_\_flowai_webclient_autoTriggerEvent = 'START_CHAT'

</script>

Find the complete code below:

<script>
 window.desktopcheck = function() {
     var check = false;
     if(window.innerWidth>768){
         check=true;
     }
     return check;
   }
 window.homepagecheck = function() {  
     var check = false;  
     if(document.location.pathname === "/"){  
         check=true;  
     }  
     return check;  
   }

 if(window.desktopcheck() && window.homepagecheck()){  
    setTimeout(function() {  
       **flowai_webclient_app.open()  
       }, 5000) // 5 secs  
    window.**flowai_webclient_autoTriggerEvent = 'START_CHAT'  
  }  
</script>