# Global Controller?

**URL:** <https://forum.ionicframework.com/t/global-controller/18023>\
**Category:** ionic-v1\
**Created:** [February 22, 2015, 5:48am UTC](https://forum.ionicframework.com/t/global-controller/18023 "2015-02-22T05:48:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cameronbourke](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cameronbourke/32/66516_2.png) [@cameronbourke](https://forum.ionicframework.com/u/cameronbourke)\
**Post date:** [February 22, 2015, 5:48am UTC](https://forum.ionicframework.com/t/global-controller/18023/1 "2015-02-22T05:48:44Z")

</div>

Just wondering what is the best way to go about implementing something similar to a global controller?

I have an `$interval` that runs in my app runs every `20s`, however I cannot put this code in a controller obviously because when I move to another view the controller’s scope will be destroyed.

So do I:

1. Add the `$interval` to `run` method in the `app.js`
2. Create a `parent controller` that wraps around the whole app? (That feels yuck just thinking that).
3. Or do I somehow not `destroy` the `controller` that currently has the `$interval` in it when that view is exited, if that makes sense.

Cheers.

---

<div class="post-metadata">

**Author:** ![cameronbourke](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cameronbourke/32/66516_2.png) [@cameronbourke](https://forum.ionicframework.com/u/cameronbourke)\
**Post date:** [February 22, 2015, 8:17am UTC](https://forum.ionicframework.com/t/global-controller/18023/2 "2015-02-22T08:17:19Z")

</div>

I’ve gone with `1.` for the moment. It is working fine, but still not sure if it is the best practice.

---

<div class="post-metadata">

**Author:** ![windht](https://avatars.discourse-cdn.com/v4/letter/w/d9b06d/32.png) [@windht](https://forum.ionicframework.com/u/windht)\
**Post date:** [February 22, 2015, 2:02pm UTC](https://forum.ionicframework.com/t/global-controller/18023/3 "2015-02-22T14:02:48Z")

</div>

You may check for the angular official example.

[https://docs.angularjs.org/api/ng/service/$interval](https://docs.angularjs.org/api/ng/service/%24interval)

Indeed you can pass the interval function to a variable, as long as the variable is not set to undefined and cancel using the $interval, it will keep on running.

So what you may need to concern is that how to close the unused interval instead of worrying about if the interval is canceled after the scope is destroyed.

My solution is to give it to an Angular Service object which may be accessible across the app and I can cancel it and start it at anytime and any place.

Hope this helps.
