當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。