以角度每 x 秒发出一次 http 请求

http request every x seconds in angular(以角度每 x 秒发出一次 http 请求)
本文介绍了以角度每 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 JavaScripts native setInterval() function.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)