簡體   English   中英

在 React 應用程序中未觸發 Observable.timer

[英]Observable.timer is not triggered in React application

我試圖每 5 秒向服務器發送一次請求,但不知何故沒有觸發計時器。 我在控制台中沒有收到任何錯誤。 知道為什么它不起作用嗎?

提前致謝 !

import React, { Component } from 'react';
import AzureService from "../_services/AzureService";
import  "../_css/TFSBuilds";
import {FormikBag as props} from "formik";
import {timer} from "rxjs";
import { switchMap } from 'rxjs/operators';



class Build extends React.Component {

    constructor(){
        super(props);
        this.state = {
            builds: []
        };
    }
     componentDidMount() {
             timer(0,5000).pipe(switchMap(()=> {

                  AzureService.send("ObCloud").then((data)=>{
                      let groupedData = this.groupBy(data.value, obj => obj.definition.id)
                      this.createChartObjects(groupedData)
                  });
                  AzureService.send("Kenevo%20Adjustment").then((data)=>{
                      let groupedData = this.groupBy(data.value, obj => obj.definition.id)
                      this.createChartObjects(groupedData)
                  });
                  AzureService.send("cockpit").then((data)=>{
                      let groupedData = this.groupBy(data.value, obj => obj.definition.id)
                      this.createChartObjects(groupedData)

                  });


              }));

        }

啊。 我現在明白了。 無需訂閱即可開展業務。

             timer(0,5000).pipe(switchMap(()=> {

例如,您可以在末尾添加.subscribe() ,或者甚至將您的.pipe(switchMap(() => /* code here */)替換為.subscribe(/* the same code */)

我認為您沒有訂閱在飛行中發送它:

嘗試:

timer(0,5000).subscribe(() => {
  AzureService.send("ObCloud").then((data)=>{
                      let groupedData = this.groupBy(data.value, obj => obj.definition.id)
                      this.createChartObjects(groupedData)
                  });
                  AzureService.send("Kenevo%20Adjustment").then((data)=>{
                      let groupedData = this.groupBy(data.value, obj => obj.definition.id)
                      this.createChartObjects(groupedData)
                  });
                  AzureService.send("cockpit").then((data)=>{
                      let groupedData = this.groupBy(data.value, obj => obj.definition.id)
                      this.createChartObjects(groupedData)

                  });

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM