• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript rxjs.interval函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中rxjs.interval函数的典型用法代码示例。如果您正苦于以下问题:TypeScript interval函数的具体用法?TypeScript interval怎么用?TypeScript interval使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了interval函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: concatMapTo2

  concatMapTo2() {
    // emit value every 2 seconds
    const interval$ = interval(2000);
    // emit value every second for 5 seconds
    const source = interval(1000).pipe(take(5));
    /*
      ***Be Careful***: In situations like this where the source emits at a faster pace
      than the inner observable completes, memory issues can arise.
      (interval emits every 1 second, basicTimer completes every 5)
    */
    //  basicTimer will complete after 5 seconds, emitting 0,1,2,3,4
    const example = interval$.pipe(
      concatMapTo(
        source,
        (firstInterval, secondInterval) => `${firstInterval} ${secondInterval}`
      )
    );
    /*
      output: 0 0
              0 1
              0 2
              0 3
              0 4
              1 0
              1 1
              continued...

    */
    const subscribe = example.subscribe(val => console.log(val));
  }
开发者ID:zwvista,项目名称:SampleMisc,代码行数:30,代码来源:transforming.service.ts


示例2: exhaustMap2

  exhaustMap2() {
    const firstInterval = interval(1000).pipe(take(10));
    const secondInterval = interval(1000).pipe(take(2));

    const exhaustSub = firstInterval
      .pipe(
        exhaustMap(f => {
          console.log(`Emission Corrected of first interval: ${f}`);
          return secondInterval;
        })
      )
      /*
        When we subscribed to the first interval, it starts to emit a values (starting 0).
        This value is mapped to the second interval which then begins to emit (starting 0).
        While the second interval is active, values from the first interval are ignored.
        We can see this when firstInterval emits number 3,6, and so on...

          Output:
          Emission of first interval: 0
          0
          1
          Emission of first interval: 3
          0
          1
          Emission of first interval: 6
          0
          1
          Emission of first interval: 9
          0
          1
      */
      .subscribe(s => console.log(s));
  }
开发者ID:zwvista,项目名称:SampleMisc,代码行数:33,代码来源:transforming.service.ts


示例3: forkJoin1

  forkJoin1() {
    const myPromise = val =>
      new Promise(resolve =>
        setTimeout(() => resolve(`Promise Resolved: ${val}`), 5000)
      );

    /*
      when all observables complete, give the last
      emitted value from each as an array
    */
    const example = forkJoin(
      // emit 'Hello' immediately
      of('Hello'),
      // emit 'World' after 1 second
      of('World').pipe(delay(1000)),
      // emit 0 after 1 second
      interval(1000).pipe(take(1)),
      // emit 0...1 in 1 second interval
      interval(1000).pipe(take(2)),
      // promise that resolves to 'Promise Resolved' after 5 seconds
      myPromise('RESULT')
    );
    // output: ["Hello", "World", 0, 1, "Promise Resolved: RESULT"]
    const subscribe = example.subscribe(val => console.log(val));
  }
开发者ID:zwvista,项目名称:SampleMisc,代码行数:25,代码来源:combining.service.ts


示例4: sample1

 sample1() {
   // emit value every 1s
   const source = interval(1000);
   // sample last emitted value from source every 2s
   const example = source.pipe(sample(interval(2000)));
   // output: 2..4..6..8..
   const subscribe = example.subscribe(val => console.log(val));
 }
开发者ID:zwvista,项目名称:SampleMisc,代码行数:8,代码来源:filtering.service.ts


示例5: interval

 observable1 = constructorZone1.run(() => {
   const source = interval(10);
   const opening = interval(25);
   const closingSelector = (v: any) => {
     expect(Zone.current.name).toEqual(constructorZone1.name);
     return v % 2 === 0 ? of(v) : empty();
   };
   return source.pipe(bufferToggle(opening, closingSelector));
 });
开发者ID:angular,项目名称:zone.js,代码行数:9,代码来源:rxjs.Observable.buffer.spec.ts


示例6: merge2

 merge2() {
   // emit every 2.5 seconds
   const first = interval(2500);
   // emit every 1 second
   const second = interval(1000);
   // used as instance method
   const example = merge(first, second);
   // output: 0,1,0,2....
   const subscribe = example.subscribe(val => console.log(val));
 }
开发者ID:zwvista,项目名称:SampleMisc,代码行数:10,代码来源:combining.service.ts


示例7: throttleTime2

 throttleTime2() {
   const source = merge(
     // emit every .75 seconds
     interval(750),
     // emit every 1 second
     interval(1000)
   );
   // throttle in middle of emitted values
   const example = source.pipe(throttleTime(1200));
   // output: 0...1...4...4...8...7
   const subscribe = example.subscribe(val => console.log(val));
 }
开发者ID:zwvista,项目名称:SampleMisc,代码行数:12,代码来源:filtering.service.ts


示例8: sample2

 sample2() {
   const source = zip(
     // emit 'Joe', 'Frank' and 'Bob' in sequence
     from(['Joe', 'Frank', 'Bob']),
     // emit value every 2s
     interval(2000)
   );
   // sample last emitted value from source every 2.5s
   const example = source.pipe(sample(interval(2500)));
   // output: ["Joe", 0]...["Frank", 1]...........
   const subscribe = example.subscribe(val => console.log(val));
 }
开发者ID:zwvista,项目名称:SampleMisc,代码行数:12,代码来源:filtering.service.ts


示例9: race1

 race1() {
   // take the first observable to emit
   const example = race<number | string>(
     // emit every 1.5s
     interval(1500),
     // emit every 1s
     interval(1000).pipe(mapTo('1s won!')),
     // emit every 2s
     interval(2000),
     // emit every 2.5s
     interval(2500)
   );
   // output: "1s won!"..."1s won!"...etc
   const subscribe = example.subscribe(val => console.log(val));
 }
开发者ID:zwvista,项目名称:SampleMisc,代码行数:15,代码来源:conditional.service.ts


示例10: interval

 'from a shared Observable', (done: MochaDone) => {
   interval(50).pipe(
     finalize(done),
     share()
   ).subscribe()
     .unsubscribe();
 });
开发者ID:DallanQ,项目名称:rxjs,代码行数:7,代码来源:finalize-spec.ts


示例11: constructor

  /**
   * @constructor
   * @param {SourceBuffer} sourceBuffer
   */
  constructor(
    bufferType : IBufferType,
    codec : string,
    sourceBuffer : ICustomSourceBuffer<T>
  ) {
    this._destroy$ = new Subject<void>();
    this.bufferType = bufferType;
    this._sourceBuffer = sourceBuffer;
    this._queue = [];
    this._currentOrder = null;
    this._lastInitSegment = null;
    this._currentCodec = codec;

    // Some browsers (happened with firefox 66) sometimes "forget" to send us
    // `update` or `updateend` events.
    // In that case, we're completely unable to continue the queue here and
    // stay locked in a waiting state.
    // This interval is here to check at regular intervals if the underlying
    // SourceBuffer is currently updating.
    interval(SOURCE_BUFFER_FLUSHING_INTERVAL).pipe(
      tap(() => this._flush()),
      takeUntil(this._destroy$)
    ).subscribe();

    fromEvent(this._sourceBuffer, "error").pipe(
      tap((err) => this._onErrorEvent(err)),
      takeUntil(this._destroy$)
    ).subscribe();
    fromEvent(this._sourceBuffer, "updateend").pipe(
      tap(() => this._flush()),
      takeUntil(this._destroy$)
    ).subscribe();
  }
开发者ID:canalplus,项目名称:rx-player,代码行数:37,代码来源:queued_source_buffer.ts


示例12: combineAll1

 combineAll1() {
   // emit every 1s, take 2
   const source = interval(1000).pipe(take(2));
   // map each emitted value from source to interval observable that takes 5 values
   const example = source.pipe(
     map(val => interval(1000).pipe(map(i => `Result (${val}): ${i}`), take(5)))
   );
   /*
     2 values from source will map to 2 (inner) interval observables that emit every 1s
     combineAll uses combineLatest strategy, emitting the last value from each
     whenever either observable emits a value
   */
   const combined = example.pipe(combineAll());
   /*
     output:
     ["Result (0): 0", "Result (1): 0"]
     ["Result (0): 1", "Result (1): 0"]
     ["Result (0): 1", "Result (1): 1"]
     ["Result (0): 2", "Result (1): 1"]
     ["Result (0): 2", "Result (1): 2"]
     ["Result (0): 3", "Result (1): 2"]
     ["Result (0): 3", "Result (1): 3"]
     ["Result (0): 4", "Result (1): 3"]
     ["Result (0): 4", "Result (1): 4"]
   */
   const subscribe = combined.subscribe(val => console.log(val));
 }
开发者ID:zwvista,项目名称:SampleMisc,代码行数:27,代码来源:combining.service.ts


示例13: publish1

  publish1() {
    // emit value every 1 second
    const source = interval(1000);
    const example = source.pipe(
      // side effects will be executed once
      tap(_ => console.log('Do Something!')),
      // do nothing until connect() is called
      publish()
    ) as ConnectableObservable<number>;

    /*
      source will not emit values until connect() is called
      output: (after 5s)
      "Do Something!"
      "Subscriber One: 0"
      "Subscriber Two: 0"
      "Do Something!"
      "Subscriber One: 1"
      "Subscriber Two: 1"
    */
    const subscribe = example.subscribe(val =>
      console.log(`Subscriber One: ${val}`)
    );
    const subscribeTwo = example.subscribe(val =>
      console.log(`Subscriber Two: ${val}`)
    );

    // call connect after 5 seconds, causing source to begin emitting items
    setTimeout(() => {
      example.connect();
    }, 5000);
  }
开发者ID:zwvista,项目名称:SampleMisc,代码行数:32,代码来源:connectable.service.ts


示例14: multicast1

  multicast1() {
    // emit every 2 seconds, take 5
    const source = interval(2000).pipe(take(5));

    const example = source.pipe(
      // since we are multicasting below, side effects will be executed once
      tap(() => console.log('Side Effect #1')),
      mapTo('Result!')
    );

    // subscribe subject to source upon connect()
    const multi = example.pipe(multicast(() => new Subject())) as ConnectableObservable<string>;
    /*
      subscribers will share source
      output:
      "Side Effect #1"
      "Result!"
      "Result!"
      ...
    */
    const subscriberOne = multi.subscribe(val => console.log(val));
    const subscriberTwo = multi.subscribe(val => console.log(val));
    // subscribe subject to source
    multi.connect();
  }
开发者ID:zwvista,项目名称:SampleMisc,代码行数:25,代码来源:connectable.service.ts


示例15: it

  it('should raise error when promise rejects', (done: MochaDone) => {
    const e1 = interval(10).pipe(take(10));
    const expected = [0, 1, 2];
    const error = new Error('error');

    e1.pipe(
      audit((x: number) => {
        if (x === 3) {
          return new Promise((resolve: any, reject: any) => { reject(error); });
        } else {
          return new Promise((resolve: any) => { resolve(42); });
        }
      })
    ).subscribe(
      (x: number) => {
        expect(x).to.equal(expected.shift()); },
      (err: any) => {
        expect(err).to.be.an('error', 'error');
        expect(expected.length).to.equal(0);
        done();
      },
      () => {
        done(new Error('should not be called'));
      }
    );
  });
开发者ID:DallanQ,项目名称:rxjs,代码行数:26,代码来源:audit-spec.ts


示例16: constructor

  constructor(private updates: SwUpdate,
              private snack_bar: MatSnackBar)
  {
    updates.available.subscribe(event => {
      console.log('current version is', event.current);
      console.log('available version is', event.available);
      let ref = snack_bar.open('Webview Update Available', 'Reload');
      ref.onAction().subscribe(() => {
        updates.activateUpdate().then(() => document.location.reload());
      });
    });
    /*
    updates.activated.subscribe(event => {
      console.log('old version was', event.previous);
      console.log('new version is', event.current);
    });
    */

    if (environment.production) {
      this.force_install();
      interval(60000).subscribe((num) => {
        this.check_now();
      });
    }
  }
开发者ID:,项目名称:,代码行数:25,代码来源:


示例17: pollForChanges

  pollForChanges(seriesImport: SeriesImportModel): Observable<SeriesImportModel> {

    let lastReceived = null;

    let seriesImportPoller = interval(1000)
      .pipe(
        flatMap(() => {
          return seriesImport.doc.reload();
        }),
        map(doc => {
          let parentAccountDoc = seriesImport.parent;
          seriesImport.init(parentAccountDoc, doc, false);
          return new SeriesImportModel(parentAccountDoc, doc);
        }),
        takeWhile((si) => {
          // HACK
          // https://github.com/ReactiveX/rxjs/issues/2420
          // TODO fixed in rxjs 6
          if (lastReceived !== null) {
            return false;
          }
          if (si.isFinished()) {
            lastReceived = si;
          }
          return true;
        })
      );

    return concat(
      observableOf(seriesImport),
      seriesImportPoller
    );
  }
开发者ID:PRX,项目名称:publish.prx.org,代码行数:33,代码来源:series-import.service.ts


示例18: constructor

 constructor(public updates: SwUpdate, public snackBar: MatSnackBar) {
   // If updates are enabled
   if (updates.isEnabled) {
     // poll the service worker to check for updates
     interval(6 * 60 * 60).subscribe(() => updates.checkForUpdate());
   }
 }
开发者ID:sivarajh,项目名称:poc,代码行数:7,代码来源:WorkerService.ts


示例19: exhaustMap1

  exhaustMap1() {
    const sourceInterval = interval(1000);
    const delayedInterval = sourceInterval.pipe(delay(10), take(4));

    const exhaustSub = merge(
      //  delay 10ms, then start interval emitting 4 values
      delayedInterval,
      //  emit immediately
      of(true)
    )
      .pipe(exhaustMap(_ => sourceInterval.pipe(take(5))))
      /*
       *  The first emitted value (of(true)) will be mapped
       *  to an interval observable emitting 1 value every
       *  second, completing after 5.
       *  Because the emissions from the delayed interval
       *  fall while this observable is still active they will be ignored.
       *
       *  Contrast this with concatMap which would queue,
       *  switchMap which would switch to a new inner observable each emission,
       *  and mergeMap which would maintain a new subscription for each emitted value.
       */
      //  output: 0, 1, 2, 3, 4
      .subscribe(val => console.log(val));
  }
开发者ID:zwvista,项目名称:SampleMisc,代码行数:25,代码来源:transforming.service.ts


示例20: takeUntil2

  takeUntil2() {
    // emit value every 1s
    const source = interval(1000);
    // is number even?
    const isEven = val => val % 2 === 0;
    // only allow values that are even
    const evenSource = source.pipe(filter(isEven));
    // keep a running total of the number of even numbers out
    const evenNumberCount = evenSource.pipe(scan((acc, _) => acc + 1, 0));
    // do not emit until 5 even numbers have been emitted
    const fiveEvenNumbers = evenNumberCount.pipe(filter(val => val > 5));

    const example = evenSource.pipe(
      // also give me the current even number count for display
      withLatestFrom(evenNumberCount),
      map(([val, count]) => `Even number (${count}) : ${val}`),
      // when five even numbers have been emitted, complete source observable
      takeUntil(fiveEvenNumbers)
    );
    /*
    	Even number (1) : 0,
      Even number (2) : 2
    	Even number (3) : 4
    	Even number (4) : 6
    	Even number (5) : 8
    */
    const subscribe = example.subscribe(val => console.log(val));
  }
开发者ID:zwvista,项目名称:SampleMisc,代码行数:28,代码来源:conditional.service.ts



注:本文中的rxjs.interval函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript rxjs.isObservable函数代码示例发布时间:2022-05-25
下一篇:
TypeScript rxjs.generate函数代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap