Condition for handling token expiration in JavaScript

How to handle the expiration of a token when receiving it from local storage, so that after it expires, it gets a new token and does not direct the user to the login page? What is the sample code?

please Put an example of the code that checks this condition