On tab-switch: So how can I call a function in the $scope that is about to be left

I have an Ionic App with 4 tabs and in each tab some screen. The first screen on one tab is used to log in. All other screens have a progress indicator to indicate how long till the user is automatically logged out.
That progress indicator is automatically reset on all user actions. Each screen has a different timeout set.
All works well when I stay in one tab, where screen switching is always done using ng-click. But when I switch a tab, I can´t stop the timeout for the progress indicator.
How can I stop a timer in a tab, before the tab is left and a new timer is set up?
So how can I call a function in the $scope that is about to be left.

I’d suggest looking into $stateChangeStart. Use it like this.
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { // Reset/delete timer here. });'

Thank you very much. It was actually quite simple: I used a service instead of a factory, and before starting a new interval timer, I stopped a previous active interval timer (clearInterval())

Cool, glad you fixed it! Mind posting a simple code example of what you did so others who have this problem in the future will know a solution?