本文整理汇总了TypeScript中rxjs.interval函数的典型用法代码示例。如果您正苦于以下问题:TypeScript interval函数的具体用法?TypeScript interval怎么用?TypeScript interval使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了interval函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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));
}
示例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));
}
示例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));
}
示例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));
}
示例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));
});
示例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));
}
示例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));
}
示例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));
}
示例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));
}
示例10: interval
'from a shared Observable', (done: MochaDone) => {
interval(50).pipe(
finalize(done),
share()
).subscribe()
.unsubscribe();
});
示例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();
}
示例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));
}
示例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);
}
示例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();
}
示例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'));
}
);
});