[RxJS] Creation Operators:interval and Timer

Source: Internet
Author: User

It is a quite common to need a Observable that ticks periodically, for instance every second or every miliseconds. We'll learn about operators interval () and timer (), both of which is similar to SetInterval () in JavaScript.

Interval (Period):

You can create interval () function by own:

varFoo =Rx.Observable.create (function (Observe) {vari =0;    SetInterval (function () {observe.next (i); I++; },  +);}) Foo.subscribe (function (x) {Console.log ('Next'+x);}, function (err) {Console.log ('Error'+err);}, function () {Console.log (' Done');});

Pre

var foo = Rx.Observable.interval (+); Foo.subscribe (function (x) {  Console.log (  " + x);}, function (err) {  console.log (' +  ERR);}, function () {  console.log ('done');});

Timer (delay/date, period):

varFoo = Rx.Observable.timer ( the, +);//After 3 second delayvarDate =NewDate (NewDate (). GetTime () + the);varFoo = Rx.Observable.timer (date, +);//accept a Date objectFoo.subscribe (function (x) {Console.log ('Next'+x);}, function (err) {Console.log ('Error'+err);}, function () {Console.log (' Done');});

[RxJS] Creation Operators:interval and Timer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.