RxJS - 如何在使用RxJS时单击按钮进行定时器触发?

时间:2016-08-29 09:45:33

标签: angular rxjs rxjs5

@Component({
    template: ` <div>
                    <a *ngIf="!sending" (click)="send()">Send</a>
                    <span *ngIf="sending" >{{seconds}} s</span>
                </div>`,
    selector: 'password-reset-form'
})
export class PasswordResetFormComponent implements OnInit {

    sending:boolean = false;
    seconds:number = 60;
    counterObservable = new Subject();


    constructor(public authService:AuthService, public router:Router, public formBuilder:FormBuilder) {

    }

    ngOnInit() {

        this.counterObservable.subscribe(()=> {
            this.seconds --;
        }, null, ()=> {
            this.seconds = 60;
            this.sending = false;
        });
    }

    send() {

        this.sending = true; 
        this.counterObservable.next(Observable.interval(1000).take(60));
    }
}

嘿,我尝试使用ng2和RxJS来制作一个计时器,当我点击发送按钮时,它会显示一个60s的计时器,但我花了很多时间仍然不知道如何使用RxJS来做它。 如果有人可以帮助我,我将非常感激。

2 个答案:

答案 0 :(得分:1)

#!/bin/bash

log_file=input
log_directory=${1-logs}

mkdir -p $log_directory

awk 'NF>1{d=l"/"$1; $1=""; print > d}' l=$log_directory $log_file

我解决了,就像这样。

答案 1 :(得分:0)

这样的事情应该有效:

@Component({
    template: ` <div>
                    <a *ngIf="!sending" (click)="send()">Send</a>
                    <span *ngIf="sending" >{{seconds}} s</span>
                </div>`,
    selector: 'password-reset-form'
})
export class PasswordResetFormComponent implements OnInit {

    sending:boolean = false;
    seconds:number = 60;    

    constructor(public authService:AuthService, public router:Router, public formBuilder:FormBuilder) {

    }

    ngOnInit() { }

    send() {

        this.sending = true;
        Observable.timer(1000, 60)
            .subscribe(() => {
                this.seconds--;
            }, 
            err => {},
            () => {
                this.sending = false;
                this.seconds = 60;
            });  
    }
}