当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Observable.interval方法代码示例

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


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

示例1: constructor

	constructor( private blockingService: BlockingServiceRequestService ) {
		const subscription1 = Observable.interval( 3000 )
			.first()
			.do( () => console.log( 'slowMethod1-1 called now' ) )
			.flatMap( () => this.blockingService.slowMethod1() )
			.subscribe( ( result ) => {
					console.log( 'slowMethod1-1 arrived:', result );
					subscription1.unsubscribe();
				}
			);

		const subscription2 = Observable.interval( 4000 )
			.first()
			.do( () => console.log( 'slowMethod2 called now' ) )
			.flatMap( () => this.blockingService.slowMethod2() )
			.subscribe(
				( result ) => {
					console.log( 'slowMethod2 arrived:', result );
					subscription2.unsubscribe();
				}
			);

		const subscription3 = Observable.interval( 2000 )
			.first()
			.do( () => console.log( 'slowMethod1-3 called now' ) )
			.flatMap( () => this.blockingService.slowMethod1() )
			.subscribe( ( result ) => {
					console.log( 'slowMethod1-3 arrived:', result );
					subscription3.unsubscribe();
				}
			);
	}
开发者ID:Plutoz01,项目名称:exp-demos,代码行数:32,代码来源:demo.component.ts

示例2: app

 function app() {
   return {
     html: Rx.Observable.interval(150).take(3).map(i =>
       div('.test-element', ['Foobar' + i]),
     ).last(),
   };
 }
开发者ID:whitecolor,项目名称:cyclejs,代码行数:7,代码来源:html-render.ts

示例3:

export const enemyShips$Fac = (enemyShips: iEnemyShip[]): Observable<iEnemyShip[]> => {
    // return Observable.of([])
  return Observable.interval(config.enemyShip.dispatchInterval)
    .scan((acc, tick) => {
      const newEnemy = {
        x: Math.random() * config.canvas.width,
        y: 0,
        directionOnX: Math.random() > 0.5 ? 1 : -1
      }
      acc.push(newEnemy)
      return acc
    }, enemyShips)
    .startWith(enemyShips)
    .switchMap(enemyShips => {
      return Observable.interval(config.commons.moveInterval)
        .map(tick => {
          enemyShips.forEach((enemyShip, index, enemyShips) => {
            if(enemyShip.x>config.canvas.width || enemyShip.x < 0) {
              enemyShip.directionOnX = enemyShip.directionOnX * -1;
            }
            enemyShip.x += config.enemyShip.moveSpeed.x * enemyShip.directionOnX;
            enemyShip.y += config.enemyShip.moveSpeed.y;
            if (enemyShip.y > config.canvas.height + config.enemyShip.halfBottomLength) {
              enemyShips.splice(index, 1);
            }
          })
          return enemyShips;
        })
    })
}
开发者ID:timathon,项目名称:rx-space,代码行数:30,代码来源:enemyShips.ts

示例4: switch

export const enemyShots$Fac = (enemyShips$: Observable<iEnemyShip[]>, enemyShots: iEnemyShot[]): Observable<iEnemyShot[]> => {
  return Observable.interval(config.enemyShot.generationInterval)
    .withLatestFrom(enemyShips$)
    .scan((acc, combo) => {
      let genTick = combo[0];
      let enemyShips = combo[1];
      if (enemyShips.length === 0) {return [...acc]}
      
      let shouldGenerate = false;
      let speed = config.enemyShot.speedYInit;

      switch(true) {
        case genTick < 30:
          if (Math.random() > 0.4) { // 60% chance, there will be a new enemyShip on each prodTick
            shouldGenerate = true;
          }
          break;
        case genTick >=30 && genTick < 90:
          if (Math.random() > 0.2) { // 80% chance, there will be a new enemyShip on each prodTick
            shouldGenerate = true;
            speed *= 1.3;
          }
          break;
        case genTick >= 90:
          shouldGenerate = true; // 100% chance, there will be a new enemyShip on each prodTick
          speed *= 2;
          break;
        default:
          break;
      }

      if (shouldGenerate) {
        let shootingEnemyShip = enemyShips[Math.floor(Math.random()*enemyShips.length)]
        let newEnemyShot = {
          x: shootingEnemyShip.x,
          y: shootingEnemyShip.y,
          speed: speed,
          collided: false
        }
        acc.push(newEnemyShot);
        return [...acc, newEnemyShot]
      } else {
        return [...acc]
      }
    }, enemyShots)
    .switchMap(enemyShots => {
      return Observable.interval(config.enemyShot.moveInterval)
        .map(moveTick => {
          enemyShots.forEach((enemyShot, index, arr) => {
            enemyShot.y += enemyShot.speed;
            if (enemyShot.y > config.canvas.height + config.enemyShot.halfBottomLength) {
              arr.splice(index, 1);
            }
          })
          return enemyShots;
        })
    })
    .startWith(enemyShots);
}
开发者ID:timathon,项目名称:rx-space,代码行数:59,代码来源:enemy-shots.ts

示例5: beforeEach

  beforeEach(() => {
    fixture = TestBed.createComponent(TimerComponent);
    component = fixture.componentInstance;

    timer = new Timer('test', 'test timer');
    component.timer = timer;
    component.clock = Observable.interval(100);

    fixture.detectChanges();
  });
开发者ID:strangesast,项目名称:scoreboard,代码行数:10,代码来源:timer.component.spec.ts

示例6:

 .switchMap(enemyShots => {
   return Observable.interval(config.enemyShot.moveInterval)
     .map(moveTick => {
       enemyShots.forEach((enemyShot, index, arr) => {
         enemyShot.y += enemyShot.speed;
         if (enemyShot.y > config.canvas.height + config.enemyShot.halfBottomLength) {
           arr.splice(index, 1);
         }
       })
       return enemyShots;
     })
 })
开发者ID:timathon,项目名称:rx-space,代码行数:12,代码来源:enemy-shots.ts

示例7: toObservable

    static toObservable(delay=2000) {
        let observableMetrics = Observable
            .interval(delay)
            .flatMap(() => {
                return Observable.from( Array.from(CommandMetricsFactory.getAllMetrics()));
            })
            .map((metrics: any) => {
                return HystrixSSEStream.toCommandJson(metrics);
            });

        return observableMetrics;
    }
开发者ID:Zenasoft,项目名称:vulcain-corejs,代码行数:12,代码来源:hystrixSSEStream.ts

示例8: main

 function main(sources: {history: Observable<Location>}) {
   return {
     history: Observable.interval(100).take(6).map(i => [
       '/test',
       '/other',
       {type: 'go', amount: -1},
       {type: 'go', amount: +1},
       {type: 'goBack'},
       {type: 'goForward'},
     ][i]),
   };
 }
开发者ID:whitecolor,项目名称:cyclejs,代码行数:12,代码来源:rxjs.ts

示例9:

 .flatMap(starArray => {
   return Observable.interval(constants.SPEED)
     .map(() => {
       starArray.forEach(star => {
         if(star.y >= StarSky.canvas.height) {
           star.y = 0;
         }
         star.y += 3;
       })
       return starArray;
     });
 });
开发者ID:graforlock,项目名称:spaceship-game,代码行数:12,代码来源:starstream.ts

示例10: switch

export const enemyShips$Fac = (enemyShips: iEnemyShip[]): Observable<iEnemyShip[]> => {
  return Observable.interval(config.enemyShip.generationInterval)
    .scan((acc, genTick) => {
      let shouldGenerate = false;
      let speed = config.enemyShip.speedYInit;
      switch(true) {
        case genTick < 30:
          if (Math.random() > 0.4) { // 60% chance, there will be a new enemyShip on each prodTick
            shouldGenerate = true;
          }
          break;
        case genTick >=30 && genTick < 90:
          if (Math.random() > 0.2) { // 80% chance, there will be a new enemyShip on each prodTick
            shouldGenerate = true;
            speed *= 1.3;
          }
          break;
        case genTick >= 90:
          shouldGenerate = true; // 100% chance, there will be a new enemyShip on each prodTick
          speed *= 2;
          break;
        default:
          break;
      }

      if (shouldGenerate) {
        let newEnemyShip = {
          x: Math.random() * config.canvas.width,
          y: 0,
          speed: speed,
          collided: false
        }
        return [...acc, newEnemyShip]
      } else {
        return [...acc]
      }
    }, enemyShips)
    .startWith(enemyShips)
    .switchMap(enemyShips => {
      return Observable.interval(config.enemyShip.moveInterval)
        .map(moveTick => {
          enemyShips.forEach((enemyShip, index, arr) => {
            enemyShip.y += enemyShip.speed;
            if (enemyShip.y > config.canvas.height + config.enemyShip.halfBottomLength) {
              arr.splice(index, 1);
            }
          });
          return enemyShips;
        })
    })

}
开发者ID:timathon,项目名称:rx-space,代码行数:52,代码来源:enemy-ships.ts


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