Unfortunately, it is not possible to read the cookie expiration time in JS so my first idea of iterating over them and executing action on their expiration dates is not possible.
If you don't want to start a digest on every "cookie check", you can disable invoking it with the fourth parameter for the $interval set to false. See the $interval docs.
Example code:
.run(function($cookies, $interval) {
var previouslyAvailableCookies = Object.keys($cookies.getAll());
var watchCookieChanges = function() {
var availableCookies = Object.keys($cookies.getAll());
var expiredCookies = previouslyAvailableCookies.filter(function(cookieName) { return availableCookies.indexOf(cookieName) < 0; });
var newCookies = availableCookies.filter(function(cookieName) { return previouslyAvailableCookies.indexOf(cookieName) < 0; });
if (expiredCookies.length || newCookies.length) {
document.writeln("<p>New cookies: " + newCookies
+ "<br>Expired cookies: " + expiredCookies + '</p>');
}
previouslyAvailableCookies = availableCookies;
};
$interval(watchCookieChanges, 3000, 0, false);
})
And a codepen.
$scope.watchwill watch cookie much more frequently thanevery 2s or 5s