Olark will automatically detect when you have agents available to chat.

However, there may be cases where you want to force Olark to display as unavailable even when you have available agents - for example, if you want to enforce particular off-hours in the evening.

Here’s how you might approach this using our JavaScript API.

  1. You’ll want to get the get the current time. The quickest way to do this is to get the visitor’s browser time:

    var now = new Date();
    var hourOfDayInUTC = now.getUTCHours();
    

    Be aware, however, that a visitor’s browser time may not be accurate. For a more robust solution, you may wish to use your server time or find an alternative method.

  2. In our case, we want to calculate the time relative to San Francisco.

    // PST is 8 hours behind GMT so we use -8. Change the -8 to
    // your timezones difference from GMT.
    var differenceFromUTC = -8;
    var currentHour = hourOfDayInUTC + differenceFromUTC;
    
  3. Using some JavaScript if statements for our own particular use case, we can finally use the API call that forces the chat box offline in the visitor’s browser:

    olark.configure("system.force_offline", true);