[英]How to show alert for slow internet in Ionic2?
I want to detect slow internet in ionic2 app and handle it. 我想在ionic2 app中检测慢速互联网并处理它。
I have done google and approach to use settimeout is perferred but how can I use it when calling an api ? 我已经完成谷歌和方法使用settimeout被允许但我怎么能在调用api时使用它?
I am calling api as: 我打电话给api:
AppSettings.API_ENDPOINT = is a constant having api url.
this.returned = this.http.post(`${AppSettings.API_ENDPOINT}login`, "email=" + email + "&password=" + password, { headers: headers }).timeout(100).map(res => res.json()).subscribe(data => {
this.conn_error_msg = '';
if (data.status == 200) {
}
else {
}
},
error => {
this.isDisabled = 0;
this.conn_error_msg = 'Internet connection error. Please try again.';
console.log(JSON.stringify(error.json()));
loader.dismiss();
}
);
My timeout is working fine using this way but i am in need to show an alert .. 使用这种方式我的超时工作正常,但我需要显示警报..
Here i wish to show an alert if time out 如果超时,我希望在这里显示警报
Solved: 解决了:
this.http.get(url)
.timeout(1000) .map(...) .subcribe((success) => {...}, (err) => { // Deal here with timeout error. }); .timeout(1000).map(...)。subsub((success)=> {...},(err)=> {//处理超时错误。});
Hope it helps someone. 希望它可以帮助某人。
Happy coding. 快乐的编码。
You can set the timeout when calling the api like this: 您可以在调用api时设置超时,如下所示:
import 'rxjs/add/operator/timeout';
return this.http.get(yourUrl).timeout(50000, new Error('Timeout!'));
Of course, the same can be done when using the post/put... methods as well. 当然,使用post / put ...方法时也可以这样做。
You can do something like: 你可以这样做:
let returned = false;
startCallTime(slowConnectionLimit){
setTimeout(() => {
if(returned){
// IS NOT SLOW
} else {
// IS SLOW. SHOW YOUR MESSAGE
}
}, slowConnectionLimit)
}
callApi = () => {
startCallTime(30000); // WILL DETECT SLOW CONNECTION AFTER 30 SECONDS
this.http.get().then((res) => {
returned = true; // HERE YOU KNOW IF THE CALL HAS ENDED
});
}
I think @sebaferreras answer is more clean, but is only useful to rxjs... 我认为@sebaferreras的答案更干净,但只对rxjs有用...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.