Google provides this functionality with Digital Wellbeing, which is pre-installed on many newer Android devices (Settings > Digital Wellbeing):
Many third-party apps do the same thing, e.g. Usage Time:
Apple’s Screen Time functionality provides this ability on iOS (Settings -> Screen Time):
To keep themselves on track, many people use a countdown timer. This is often done with the ‘Pomodoro’ technique, where you break your work down in intervals, typically 25 minutes, separated by short breaks.
Numerous tools support this, for example Be Focused:
Some website blockers also include Pomodoro timer functionality, for example the Chrome extension FocusMe:
On Firefox the Pomodoro clock extension gives you a pomodoro timer in the browser:
Try e.g. Mattie: