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


#1

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.


#2

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:
https://docs.angularjs.org/api/ng/service/$interval

<html>
  <head>
    ...
  </head>
  <body ng-controller="MainCtrl">
    ...
  </body>
</html>

#3

Thanks, its working :smile:


#4

Hi,

You should create a service for that !
https://docs.angularjs.org/guide/services

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

  return service;
})

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

.run(function(ClockSrv){
  ClockSrv.startClock(function(){
    // things to do ...
  });
})

#5

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.


#6

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
    };
    clockElts.push(elt);
    if(clockElts.length === 1){ startClock(); }
    return elt.id;
  }
  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){
          clockElts[i].fn();
        }
      }, 1000);
    }
  }
  function stopClock(){
    if(clockTimer !== null){
      $interval.cancel(clockTimer);
      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…


#7

:smiley: Thank you loicknuchel


#8

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.


#9

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


#10

is there any guide for ionic/angular 2? plz


#11

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

setInterval(data=>{
  //the code
},1000);

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


#12

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


#13

thanks to all, solutions was so helpful,


#14

@nexi yes a very good idea too with full rxjs


#15

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