$ watch in Typescript

0

I need that after the application starts it runs a set of functions every 20 seconds or every time a json is updated, in angle 1 had the $ watch that could be used for such functionality, in angular 2 I do not know if it has something similar, it may not be the best way, I tried with a while, but obviously it caught everything

    
asked by anonymous 17.08.2017 / 21:11

1 answer

1

You can use the javascript native setInterval function to run something indefinitely for a period of time.

But every time a JSON changes I suggest access to the file system, which you do not have browser access to. Maybe the service you use should send a notification to indicate that there was a change. I believe that any solution that does this type of monitoring or uses the time for verification or receives a notification. I have taken a look at the $ watch specification and it does something like this (time x notification).

See example of using setInterval:

@Component()
export class MeuComponent implements OnDestroy, AfterViewInit
{
    private tokenInterval: any;

    ngAfterViewInit()
    {
       let tempoSeg = 1;
       this.tokenInterval = setInterval(()=> this.minhaF(), tempoSeg * 1000);

    }

    ngOnDestroy()
    {
       if (!!this.tokenInterval) clearInterval(this.tokenInterval);
    }

    private minhaF()
    {

       console.log('minhaF executada');

    }




}
    
17.08.2017 / 23:02