繁体   English   中英

如何在Ionic2中显示慢速互联网的警报?

[英]How to show alert for slow internet in Ionic2?

我想在ionic2 app中检测慢速互联网并处理它。

我已经完成谷歌和方法使用settimeout被允许但我怎么能在调用api时使用它?

我打电话给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();
        }
        );

使用这种方式我的超时工作正常,但我需要显示警报..

如果超时,我希望在这里显示警报

解决了:

this.http.get(url)

.timeout(1000).map(...)。subsub((success)=> {...},(err)=> {//处理超时错误。});

希望它可以帮助某人。

快乐的编码。

您可以在调用api时设置超时,如下所示:

import 'rxjs/add/operator/timeout';

return this.http.get(yourUrl).timeout(50000, new Error('Timeout!'));

当然,使用post / put ...方法时也可以这样做。

你可以这样做:

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
 });
}

我认为@sebaferreras的答案更干净,但只对rxjs有用...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM