<i id='WTRTc'><tr id='WTRTc'><dt id='WTRTc'><q id='WTRTc'><span id='WTRTc'><b id='WTRTc'><form id='WTRTc'><ins id='WTRTc'></ins><ul id='WTRTc'></ul><sub id='WTRTc'></sub></form><legend id='WTRTc'></legend><bdo id='WTRTc'><pre id='WTRTc'><center id='WTRTc'></center></pre></bdo></b><th id='WTRTc'></th></span></q></dt></tr></i><div id='WTRTc'><tfoot id='WTRTc'></tfoot><dl id='WTRTc'><fieldset id='WTRTc'></fieldset></dl></div>

    1. <tfoot id='WTRTc'></tfoot>

      <small id='WTRTc'></small><noframes id='WTRTc'>

        <bdo id='WTRTc'></bdo><ul id='WTRTc'></ul>
    2. <legend id='WTRTc'><style id='WTRTc'><dir id='WTRTc'><q id='WTRTc'></q></dir></style></legend>
    3. 以角度每 x 秒发出一次 http 请求

      时间:2023-09-07

      <tfoot id='zQaXL'></tfoot>

        • <i id='zQaXL'><tr id='zQaXL'><dt id='zQaXL'><q id='zQaXL'><span id='zQaXL'><b id='zQaXL'><form id='zQaXL'><ins id='zQaXL'></ins><ul id='zQaXL'></ul><sub id='zQaXL'></sub></form><legend id='zQaXL'></legend><bdo id='zQaXL'><pre id='zQaXL'><center id='zQaXL'></center></pre></bdo></b><th id='zQaXL'></th></span></q></dt></tr></i><div id='zQaXL'><tfoot id='zQaXL'></tfoot><dl id='zQaXL'><fieldset id='zQaXL'></fieldset></dl></div>
          • <bdo id='zQaXL'></bdo><ul id='zQaXL'></ul>
              <tbody id='zQaXL'></tbody>
          • <legend id='zQaXL'><style id='zQaXL'><dir id='zQaXL'><q id='zQaXL'></q></dir></style></legend>

            <small id='zQaXL'></small><noframes id='zQaXL'>

                本文介绍了以角度每 x 秒发出一次 http 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试在 angular2 中每 x 秒刷新一次 http 调用.

                I'm trying to refresh an http call every x seconds in angular2.

                  ionViewDidLoad() {
                
                    let loader = this.LoadingController.create({
                      'content':'Please Wait'
                    });
                    loader.present().then(()=>{
                      this.http.request('http://mywebserver.com/apps/random.php').map(res=> res.json()).subscribe(data=>{
                        console.log(JSON.stringify(data));
                        loader.dismiss();
                        this.fact = data;
                      },err=>{
                        loader.dismiss();
                        let alert = this.alertCtrl.create({
                          title: 'Error!',
                          subTitle: 'Please check your Internet Connectivity',
                          buttons: ['OK']
                        });
                      alert.present();
                    })
                    })
                
                  }
                

                我在页面新加载时获取数据.但现在我的问题是刷新 http 调用以每 x 秒获取新数据

                I get data when the page newly loads. But now my issue is refreshing the http call to get new data every x seconds

                推荐答案

                使用 Observable.interval:

                import {Observable} from 'rxjs/Rx';
                ...
                constructor(...) {
                  Observable.interval(30000).subscribe(x => { // will execute every 30 seconds
                    this.ionViewDidLoad();
                  });
                }
                

                或者在你的 ionViewDidLoad 函数中:

                OR inside your ionViewDidLoad function:

                Observable.interval(3000)
                          .timeInterval()
                          .flatMap(() => this.http.request('http://mywebserver.com/apps/random.php')
                          .map(res=> res.json())
                          .subscribe(data=>{
                              console.log(JSON.stringify(data));
                              loader.dismiss();
                              this.fact = data;
                            });
                

                编辑以回答您的评论.来自 Rxjs 文档:

                Edit to answer your comment. From the Rxjs docs:

                timeInterval 运算符将源 Observable 转换为Observable 发出指示之间经过的时间量的指示源 Observable 的连续发射.第一次发射从这个新的 Observable 中可以看出两者之间经过的时间量观察者订阅 Observable 的时间和时间当源 Observable 发出它的第一项时.没有相应的排放标记之间经过的时间量源 Observable 的最后一次发射和随后的调用已完成.

                The timeInterval operator converts a source Observable into an Observable that emits indications of the amount of time lapsed between consecutive emissions of the source Observable. The first emission from this new Observable indicates the amount of time lapsed between the time when the observer subscribed to the Observable and the time when the source Observable emitted its first item. There is no corresponding emission marking the amount of time lapsed between the last emission of the source Observable and the subsequent call to onCompleted.

                timeInterval 默认操作超时Scheduler,但也有一个变体,允许您通过传递它来指定调度程序in 作为参数.

                timeInterval by default operates on the timeout Scheduler, but also has a variant that allows you to specify the Scheduler by passing it in as a parameter.

                在这种情况下,它基本上是 JavaScript 原生 setInterval() 函数的响应式/Rxjs 方式.

                Basically in this case it would be the reactive/Rxjs way of JavaScript´s native setInterval() function.

                这篇关于以角度每 x 秒发出一次 http 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Ionic 2:隐藏滚动条并继续滚动 下一篇:将我的自定义图标添加到 ionic 2 actionSheet 的按钮

                相关文章

                最新文章

                <i id='3PgaK'><tr id='3PgaK'><dt id='3PgaK'><q id='3PgaK'><span id='3PgaK'><b id='3PgaK'><form id='3PgaK'><ins id='3PgaK'></ins><ul id='3PgaK'></ul><sub id='3PgaK'></sub></form><legend id='3PgaK'></legend><bdo id='3PgaK'><pre id='3PgaK'><center id='3PgaK'></center></pre></bdo></b><th id='3PgaK'></th></span></q></dt></tr></i><div id='3PgaK'><tfoot id='3PgaK'></tfoot><dl id='3PgaK'><fieldset id='3PgaK'></fieldset></dl></div>

                <small id='3PgaK'></small><noframes id='3PgaK'>

                    <bdo id='3PgaK'></bdo><ul id='3PgaK'></ul>

                1. <tfoot id='3PgaK'></tfoot>
                2. <legend id='3PgaK'><style id='3PgaK'><dir id='3PgaK'><q id='3PgaK'></q></dir></style></legend>