javascript - How to write a function with an Observable? -
i want create function returns observable
.the description of function follows:
the function should
delayedalert(message:string, time:number)
returnsobservable
.the function should contain
settimeout
function insidedelayedalert
printsmessage
after set 'time
' value.
ex:
delayedalert(message, time){ return new observable//how implement settimeout function here?
use observable.create
create observable, , in first callback, write logic populate observable, in case settimeout
.
function delayedalert(msg, time) { return observable.create( observer => settimeout(() => observer.onnext(msg), time)); }
then use it:
delayedalert("hi, sally", 1000).subscribe(msg => alert(msg));
however, if using observables, don't need use settimeout
; use delay
instead, applied of
, creates observable individual value(s):
function delayedalert(msg, time) { return observable.of(msg).delay(time); }
since it's easy write way, don't need delayedalert
function @ all:
const observable = observable.of("hi, sally").delay(1000); observable.subscribe(msg => alert(msg));
Comments
Post a Comment