# Create cron to execute an action

**URL:** <https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092>\
**Category:** ionic-v1\
**Created:** [February 23, 2015, 1:35pm UTC](https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092 "2015-02-23T13:35:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![sioesi](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sioesi](https://forum.ionicframework.com/u/sioesi)\
**Post date:** [February 23, 2015, 1:35pm UTC](https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092/1 "2015-02-23T13:35:04Z")

</div>

I need to perform an action while the device is locked. I have taken the setInvertal function and works well, but at some point, take a few minutes, this function stops running without reason, do not throw any error in the browser nor in GapDebug.

```
var myInterval = setInterval(function(){update()}, 10000);

```

Within this function, I make a post to a webservice, checking log into the server, at some point, stop called but somewhere between 2-5 minutes cron, hence runs perfect for front stop calling .

---

<div class="post-metadata">

**Author:** ![BioPhoton](https://avatars.discourse-cdn.com/v4/letter/b/a3d4f5/32.png) [@BioPhoton](https://forum.ionicframework.com/u/BioPhoton)\
**Post date:** [February 23, 2015, 3:14pm UTC](https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092/2 "2015-02-23T15:14:19Z")

</div>

Try uesing the [$interval](https://docs.angularjs.org/api/ng/service/%24interval) module. maybe this helps…

---

<div class="post-metadata">

**Author:** ![sioesi](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sioesi](https://forum.ionicframework.com/u/sioesi)\
**Post date:** [February 23, 2015, 3:29pm UTC](https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092/3 "2015-02-23T15:29:29Z")

</div>

The problem persists @BioPhoton

---

<div class="post-metadata">

**Author:** ![BioPhoton](https://avatars.discourse-cdn.com/v4/letter/b/a3d4f5/32.png) [@BioPhoton](https://forum.ionicframework.com/u/BioPhoton)\
**Post date:** [February 23, 2015, 3:37pm UTC](https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092/4 "2015-02-23T15:37:19Z")

</div>

what exactly happens when the interval stops? does the phone goes into standby?

---

<div class="post-metadata">

**Author:** ![BioPhoton](https://avatars.discourse-cdn.com/v4/letter/b/a3d4f5/32.png) [@BioPhoton](https://forum.ionicframework.com/u/BioPhoton)\
**Post date:** [February 23, 2015, 3:40pm UTC](https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092/5 "2015-02-23T15:40:00Z")

</div>

could you post your code please?

---

<div class="post-metadata">

**Author:** ![sioesi](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sioesi](https://forum.ionicframework.com/u/sioesi)\
**Post date:** [February 23, 2015, 3:47pm UTC](https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092/6 "2015-02-23T15:47:13Z")

</div>

@BioPhoton What happens: The device is locked, the action is executed normally for about 2-5 minutes. Then stops working.

```
var notificacion = $interval(function(){
    $scope.notificaciones();
 },1000);

$scope.notificaciones = function(){
if(navigator.onLine){
  lista_corredores = misCorredoresService.getCorredores();
  for (var i = 0; i < lista_corredores.length; i++) {
      pid = lista_corredores[i].pid;
      $http({method: 'GET', url: URL_ESTADISTICAS+lista_corredores[i].pid+'/splits?pidversion=1&igt='+lista_corredores[i].pid+'&appid='+appId+'&token='+tokenApp})
      .success(function(data){
          if(data.error){
            if(data.error.type=="no_results" && data.error.msg=="No splits found"){
                
            }
          }
          list_2 = data.list;
          pasa = true;
          lista = data.list;
          lista_anterior = sessionService.get('tiempos_corredor'+lista[0].pid);
          ultimo = lista.length;
          if(lista_anterior!=null){
            ultimo_anterior = lista_anterior.length;
            if(lista_anterior == lista[ultimo-1].time){
               pasa = false;
            }else{
              pasa = true;
              sessionService.set('tiempos_corredor'+lista[0].pid,lista[ultimo-1].time);
            }
            console.log("Pasa "+pasa+" corredor "+lista[ultimo-1].pid
            console.log(sessionServiceToken.get('token_device')););
            Enviar notificacion
              console.log("http://dev.lixsys.cl/~nhernandez/pushnotificacion.php?corredor="+lista[ultimo-1].name+"&label="+lista[ultimo-1].label+"&token_device="+sessionServiceToken.get('token_device')); if(pasa){
              //Send notification
              $http({method: 'GET', url: 'pushnotificacion.php?corredor='+lista[ultimo-1].name+'&label='+lista[ultimo-1].label+'&token_device='+sessionServiceToken.get('token_device')})
                .success(function(data){
                  
                })
                .error(function(){
                  
                });
            }
          }else{
            sessionService.set('tiempos_corredor'+lista[0].pid,lista[ultimo-1].time);
            $http({method: 'GET', url: 'pushnotificacion.php?corredor='+lista[ultimo-1].name+'&label='+lista[ultimo-1].label+'&token_device='+sessionServiceToken.get('token_device')})
                .success(function(data){
                  
                })
                .error(function(){
                  
                });
          }
      })
      .error(function(){

      });
     }
   }
 }
```

---

<div class="post-metadata">

**Author:** ![BioPhoton](https://avatars.discourse-cdn.com/v4/letter/b/a3d4f5/32.png) [@BioPhoton](https://forum.ionicframework.com/u/BioPhoton)\
**Post date:** [February 23, 2015, 6:19pm UTC](https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092/7 "2015-02-23T18:19:43Z")

</div>

Setup logging function that loggs every interesting step in your code with in a scope var.  
Key them by timestamp.  
And print this out on your phones screen.

---

<div class="post-metadata">

**Author:** ![sioesi](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sioesi](https://forum.ionicframework.com/u/sioesi)\
**Post date:** [February 23, 2015, 6:23pm UTC](https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092/8 "2015-02-23T18:23:13Z")

</div>

@BioPhoton I got this information in another post and it worked wonders 🙂 I leave around here in case someone needs it.

thank you very much for your help !!!

> **[katzer/cordova-plugin-background-mode](https://github.com/katzer/cordova-plugin-background-mode)**
>
> cordova-plugin-background-mode - Keep app running in background

---

<div class="post-metadata">

**Author:** ![BioPhoton](https://avatars.discourse-cdn.com/v4/letter/b/a3d4f5/32.png) [@BioPhoton](https://forum.ionicframework.com/u/BioPhoton)\
**Post date:** [February 23, 2015, 6:25pm UTC](https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092/9 "2015-02-23T18:25:59Z")

</div>

Cool! Im happy for you! 😄

---

<div class="post-metadata">

**Author:** ![sioesi](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sioesi](https://forum.ionicframework.com/u/sioesi)\
**Post date:** [February 23, 2015, 6:27pm UTC](https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092/10 "2015-02-23T18:27:12Z")

</div>

and it was killing me xD, the mistake I make is that no database management, it is a problem to do what I wanted, which was to send notifications but this was solved 🙂

---

<div class="post-metadata">

**Author:** ![brunodb3](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@brunodb3](https://forum.ionicframework.com/u/brunodb3)\
**Post date:** [February 23, 2015, 8:06pm UTC](https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092/11 "2015-02-23T20:06:14Z")

</div>

Have you encountered any issues with battery life using this plugin? It sounds interesting and could help me solve a big issue… Thanks 😉

---

<div class="post-metadata">

**Author:** ![sioesi](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sioesi](https://forum.ionicframework.com/u/sioesi)\
**Post date:** [February 23, 2015, 8:10pm UTC](https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092/12 "2015-02-23T20:10:24Z")

</div>

tests and you’ll do tomorrow comment 😉

---

<div class="post-metadata">

**Author:** ![brunodb3](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@brunodb3](https://forum.ionicframework.com/u/brunodb3)\
**Post date:** [February 23, 2015, 8:12pm UTC](https://forum.ionicframework.com/t/create-cron-to-execute-an-action/18092/13 "2015-02-23T20:12:10Z")

</div>

Sounds cool, thanks 😄
