本文整理汇总了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();
}
);
}
示例2: app
function app() {
return {
html: Rx.Observable.interval(150).take(3).map(i =>
div('.test-element', ['Foobar' + i]),
).last(),
};
}
示例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;
})
})
}
示例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);
}
示例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();
});
示例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;
})
})
示例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;
}
示例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]),
};
}
示例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;
});
});
示例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;
})
})
}