In my app i am saving data by form and calling Api for the same, to check if their is slow internet connection i am using timeout in ionic2 as:
savedata(){
let headers = new Headers();
headers.set('Content-Type', 'application/x-www-form-urlencoded')
this.http.post(`api-url to save data`, "access_token=" + access_token + "&user_id=" + id + "&username=" + username, { headers: headers }).timeout(30000).map(res => res.json()).subscribe(data => {
if(data.status == 200)
{
console.log("data saved.");
}
else
{
console.log("Error saving data.");
}
},error => {
// if their is an internet issue
// on timeout
this.response_message = 'Internet connection error. Please try again.';
});
}
as form is submitted savedata() function is triggered.
The issue is that if their is slow internet it comes in error and error msg is shown that is correct but the request continue to process background(not visible to user) and as he changes his internet connection, save api is called and data enters in database, what i need is that if their is timeout cancel out that request also with error message.
Can any one please suggest how can i achieve the same.
Thanks in advance.