Hi,
you can do it with timestamps.
There is the "browser-object" which can detect if your window is unfocused.
If you are interested in a full idle game script made with construct 2, here you can buy it very cheap:
codecanyon.net/item/clicker-upgrade-idle-game-html5-app/11666827
There are all these functions included (unfocused window, offline mode...)
Here the link to the game how it looks like:
kongregate.com/games/CreativeGamez/island-clicker
Best regards.