How to force log out after session expired?

Currently if the session token is expired, there’s no way for the application to know. We have to wait until they make any request after that to know their session has timed out, and redirect them to login again. Is there a way to check for the expiration in the background, and if the token is expired, force them out immediately? I was thinking about setTimeout, but could not really come up with an implementation yet.