Skip to main content

delay

函数签名: delay(delay: number | Date, scheduler: Scheduler): Observable

根据给定时间延迟发出值。

示例

示例 1: 延迟时间持续增加

( StackBlitz | jsBin | jsFiddle )

// RxJS v6+
import { of, merge } from 'rxjs';
import { mapTo, delay } from 'rxjs/operators';

// 发出一项
const example = of(null);
// 每延迟一次输出便增加1秒延迟时间
const message = merge(
example.pipe(mapTo('Hello')),
example.pipe(
mapTo('World!'),
delay(1000)
),
example.pipe(
mapTo('Goodbye'),
delay(2000)
),
example.pipe(
mapTo('World!'),
delay(3000)
)
);
// 输出: 'Hello'...'World!'...'Goodbye'...'World!'
const subscribe = message.subscribe(val => console.log(val));

其他资源


📁 源码: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/delay.ts