Reputation: 55
I am developing a project where I need to check for change in a json file. I have implemented it as:
import { Subscription } from 'rxjs';
import { HttpClient } from "@angular/common/http";
variables that hold the data are declared as:
var data4:any = [];
var flag2:boolean = false;
and in the export class section
ngOnInit()
{
setInterval(()=>
{
this.httpClient.get("../../assets/data/abc.json").subscribe(data3 =>
{
if((JSON.stringify(data3) != JSON.stringify(data4) && flag2))
{
this.loadData(60); /**/
}
else
{
flag2 = true;
}
data4 = data3;
})
},10)
}
The code is enormous so I have pasted only the relevant part.
When I change the value in the json file, the loadData function does get executed but only after a while.
Is there anything that i can do to eliminate or reduce this lag?
Please guide
Upvotes: 1
Views: 221
Reputation: 840
Is there anything that i can do to eliminate or reduce this lag?
We need more information to answer that. First, you have to understand that the lag is due the http call. How to reduce the time to response? Well, you'll have to show us what's going on on the server side of this call:
this.httpClient.get("../../assets/data/abc.json")
Are you the owner of that endpoint?
Secondly, you are firing http request each 10 milliseconds. That's too fast for an http request, you are basically doing 100 requests per second, I don't think http was designed for that, in my humble opinion if you really need realtime response to those changes, then use a websocket instead, else just increase that delay between calls.
Upvotes: 1