zoukankan      html  css  js  c++  java
  • [RxJS] Creation operators: interval and timer

    It is quite common to need an Observable that ticks periodically, for instance every second or every 100 miliseconds. We will learn about operators interval() and timer(), both of which are similar to setInterval() in JavaScript.

    Interval(period):

    You can create interval() function by you own:

    var foo = Rx.Observable.create( function(Observe){
      var i = 0;
      setInterval(function(){
        Observe.next(i);
        i++;
      }, 1000);
    })
    
    foo.subscribe(function (x) {
      console.log('next ' + x);
    }, function (err) {
      console.log('error ' + err);
    }, function () {
      console.log('done');
    });

    Or:

    var foo = Rx.Observable.interval(1000);
    
    foo.subscribe(function (x) {
      console.log('next ' + x);
    }, function (err) {
      console.log('error ' + err);
    }, function () {
      console.log('done');
    });

    timer(delay/date, period):

    var foo = Rx.Observable.timer(3000, 1000); // after 3 second delay
    var date = new Date(new Date().getTime() + 3000);
    var foo = Rx.Observable.timer(date, 1000); // accept a date object
    
    foo.subscribe(function (x) {
      console.log('next ' + x);
    }, function (err) {
      console.log('error ' + err);
    }, function () {
      console.log('done');
    });
  • 相关阅读:
    ldconfig和ldd用法
    Linux上ld和ld.so命令的区别
    一维二维码的提取、识别和产生
    最大轮廓和投影
    如何做出半透明和闪光效果
    马赫效应和应对方法
    钢管识别项目1
    钢管识别项目2
    选择轮廓(select_shape)
    压板识别项目分析
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5401372.html
Copyright © 2011-2022 走看看