javascript - How to write a function with an Observable? -


i want create function returns observable.the description of function follows:

  1. the function should delayedalert(message:string, time:number) returns observable.

  2. the function should contain settimeout function inside delayedalert prints message 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

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -