简体   繁体   中英

How to setup an RxJS.Observable.timer() with a due date

I have an authentication system here which has several information, including an expires_in property, declared in seconds. This informs when the token generated will be expired and, to handle this I'm using the concept of an RxJS.Observable .

With the expires_in value, I calculate the expiration date generating a Date object, incrementing it's value with the expiration, and getting it's time with getTime() , in milisseconds, and with this I have the due date.

When I declare the RxJS.Observable , I'm doing like this:

// The code is shortened to the relevant part

let obs = Observable.timer(dueVar) // Which is in milisseconds
    .subscribe(
        (x) => console.log(x),
        (x) => console.log(x),
        () => console.log('Expired') // onComplete, right? Only this one matters to me...
    );

I've tried to put, where is dueVar , the Date object, but no success at all... The snippet throws the first parameter of the subscribe() function as 0 [Number] , and immediately throws Expired , which is the last param.

How can I make this operation? I didn't any relevant explanation for that matter... The second parameter, as I saw on the docs, it's optional, and for call the onNext callback every span time that is declared there. But I need to dispatch this one time only.

On the comments, it became more clear what I needed to do.

I was considering the time in an absolute way, but if I'll use milisseconds, as @paulpdaniels stated, the time need to be relative from now , which makes a huge difference.

So, despite informing the argument with the entire timestamp from 1970's, I only need to use the expires_in property, times 1000, to transform it from seconds to milisseconds.

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