Back to Blog

How to combine functions

Java script can be used for several tasks such as detecting if the user is on a Desktop or Mobile device or to check if the user is on theHomepage or a Specific Page. In this example we want to combine these two functions. If the user is on the homepage on a desktop device, we want to trigger a specific event after 5 seconds.

  1. Create functions
  2. Combine functions
  3. Add delayed opening & trigger event
Desktop and Mobile Chatbot

1. Create functions

We start by creating two functions. For more details about these functions have a look at:


<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>

2. Combine functions

To combine function we want both functions to return true.

<script>

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

</script>     

3. Add delayed opening and start event

Now let’s add the delayed opening and trigger an event.

<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>
Start for freeContact us