Skip to content

Instantly share code, notes, and snippets.

@enricop89
Created October 8, 2018 11:03
Show Gist options
  • Save enricop89/b13c1cfbf83b1c77040281141deeb006 to your computer and use it in GitHub Desktop.
Save enricop89/b13c1cfbf83b1c77040281141deeb006 to your computer and use it in GitHub Desktop.
Bandyer chat logout after timeout
/**
* This function calls the BandyerChat.logout() method after a specified timeout (default: 5 minutes)
* If BandyerChat widget has not been initialized, the function will throw an error.
* @param timeout
* @param callback
*/
var exitWidgetAfterTimeout = function (timeout, callback) {
if (BandyerChat) {
var delay = timeout || (5* 60 * 1000);
setTimeout(function() {
try {
var unreadMessages = BandyerChat.getUnreadMessages();
var haveUnreadMessages = false;
for (var i = 0; i < unreadMessages.length; i++) {
if (unreadMessages[i].unreadMessages > 0) {
haveUnreadMessages = true;
}
}
if (!haveUnreadMessages) {
BandyerChat.logout();
}
if (callback) {
callback();
}
} catch (e) {
throw e;
}
}, delay);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment