If you need to achieve this in a foolproof way that the user can't tamper with, you'll have to set the timer serverside.
If it's not that important, then you can store the start-time in localStorage and, on page load, use that value to resume the timer from where it began.
var oldStartTime = localStorage.getItem('startTime');
var startTime = oldStartTime ? new Date(oldStartTime) : new Date();
localStorage.setItem('startTime', startTime);
// you can now use startTime to determine the remaining duration for setTimeout:
var elapsed = new Date() - startTime;
var duration = 180000 - elapsed;
setTimeout(myFunction, duration);
Since you said it's an exam though, I assume you want people to not be able to cheat. If that's the case then there is literally no way to do this with client-side Javascript alone. It is always possible for the user to "tamper" if it's occurring in their own browser.