How to call a function at specific time interval in application instead of controller level?

well i m beginner in angularjs and ionic framework,

what i want to do is that i want to get user location after each 5 minutes so i need to use a function on time interval but i want to use it globally means not at just controller level. it is like it will be called after each 5 minutes automatically doesnt matter to in which controller we are.

Thank you.

I would do it stil on controller level.
In most cases you have a “main controller” wrapped around your content like a controller on the body tag.
Or if you have an abstract base-state you could connect this state with this controller.

And for the interval:$interval

  <body ng-controller="MainCtrl">

Thanks, its working :smile:


You should create a service for that !

.factory('ClockSrv', function($interval){
  var clock = null;
  var service = {
    startClock: function(fn){
      if(clock === null){
        clock = $interval(fn, 5000);
    stopClock: function(){
      if(clock !== null){
        clock = null;

  return service;

You can start it on the angular run method like this :

    // things to do ...

Maybe it would be nice to build this factory like an own class where you can create instances of… to provide the possibility to handle multiple intevals ;).

If i understand your code correctly, you will overwrite your “clock” everytime you start a new interval.

Yes… In fact this is an (over)simplification of what I do in my code.
If you start multiple timers, it can have some performance issues and all your clocks are not sync (quite ugly for user !).

So it’s interesting to have just one clock for multiple timers.
Here is my full code :

.factory('ClockSrv', function($interval){
  'use strict';
  var service = {
    clock: addClock,
    cancelClock: removeClock
  var clockElts = [];
  var clockTimer = null;
  var cpt = 0;
  function addClock(fn){
    var elt = {
      id: cpt++,
      fn: fn
    if(clockElts.length === 1){ startClock(); }
  function removeClock(id){
    for(var i in clockElts){
      if(clockElts[i].id === id){
        clockElts.splice(i, 1);
    if(clockElts.length === 0){ stopClock(); }
  function startClock(){
    if(clockTimer === null){
      clockTimer = $interval(function(){
        for(var i in clockElts){
      }, 1000);
  function stopClock(){
    if(clockTimer !== null){
      clockTimer = null;
  return service;

With this service, you can have as many timers as you want, with just one clock and they are all sync !

This service only handle clocks with 1 second delay but that’s only what I need. If you need more, you can complexify it by having an array of clockTimers and an array of array of clockElts with each requested interval…

:smiley: Thank you loicknuchel

will it work if app is closed and opened again? if not can you please explain how to do that.In my app i want to run a function once in a day.

What should this function do??? is it really necessary… also if the app is not open??

is there any guide for ionic/angular 2? plz

im also make an api project that will get user location each 1 sec, with code

  //the code

its working with http get and post.
hope it help :slight_smile:


You can use mahluzs suggestion or use observables like i wrote about here:

thanks to all, solutions was so helpful,

@nexi yes a very good idea too with full rxjs

Hello, would you be able to provide the same class factory in typescript.
Thank you.

thanks, It’s working perfect