skip to Main Content

I currently have a refresh token API call after every 60 mints,

Here is my condition:

 **const expires_in = 3600 //This is 3600s comes from auth api 
if (refreshToken && expires_in * 1000 > Date.now() + 3600000) {
// RefreshToken Function
else {
        console.log("accessToken not expired yet");
    }**

This condition is not work properly. This condition call RefreshToken Function on every reload but I want to call this after every 60 minutes.

Accurate my condition which is call after every 60 minutes.

2

Answers


  1. Can you not just use a sleep function, or something like that ?

    Login or Signup to reply.
  2. expires_in * 1000 = 3600000 ,

    Date.now() + 3600000 = 1720672843350 ,

    So the condition can never be true.
    Also your could should look like:

        const expires_in = 3600 //This is 3600s comes from auth api 
         if ((refreshToken) && (expires_in * 1000 > Date.now() + 3600000)) {
       // RefreshToken Function
    
         }else {
    
        console.log("accessToken not expired yet");
      }
    

    In this case you are expecting both conditions to be true to execute the function.
    But here as I told you :
    The first condition can never be greater than the second one, which means you will never be able to execute the function according to your condition.
    Maybe you wanted to say:

        const expires_in = 3600 //This is 3600s comes from auth api 
     if ((refreshToken) && (expires_in * 1000 + Date.now() > Date.now() + 3600000)) 
    {
      // RefreshToken Function
    
     }else {
    
    console.log("accessToken not expired yet");
    

    }

    My advice is to correct the math first and figure out what exactly should the condition be like.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search