简体   繁体   中英

Why does this console log 0?

Can anyone help me with this? I can't understand why this code console logs 0. Thanks

const button = document.querySelector('button');

const observable = Rx.Observable.fromEvent(button, 'click');
observable
  .switchMap(event => Rx.Observable.timer(1000))
  .subscribe(
      (data) => console.log(data)
    );

On button click you switch your button event to the timer Observable via switchMap . As a result you get timer event in the subscribe. In your example timer event will be number 0, because it is how timer works.

Have a look at timer declaration, it emits numbers starting from 0, but in your case timer will just emit one number and stop, you can control this with the second parameter of timer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM