0

I'm creating online exam kind of application and in my app

when user clicks on start button we are going to display timer for 3:00 minutes

using setTimeout(func,3*60*1000)

but when I refresh the page the timout function is resetting, how to stop this

how to do this , this is a must have feature in my app.

Javascript or jquery anything welcomes

user1934044
  • 516
  • 5
  • 18
  • possible duplicate of [jquery beforeunload when closing (not leaving) the page?](http://stackoverflow.com/questions/18783535/jquery-beforeunload-when-closing-not-leaving-the-page) – jogesh_pi Oct 22 '14 at 03:44
  • Hope this post would be helpful http://stackoverflow.com/questions/3121929/countdown-timer-with-cookies http://stackoverflow.com/questions/13412153/countdown-timer-that-doesnt-reset-when-you-refresh-the-page – Sameera Thilakasiri Oct 22 '14 at 03:46

1 Answers1

0

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.

Semicolon
  • 6,793
  • 2
  • 30
  • 38
  • http://keith-wood.name/countdown.html, this looks simple, Is there any issues with this plugin – user1934044 Oct 22 '14 at 04:09
  • That jQuery plugin is for rendering HTML for a countdown to a specified time. The only reason it doesn't "reset" is because it's counting down to a specific moment; it's not like a stopwatch. If your students are going to begin the tests at different times, you still need to store the start time. – Semicolon Oct 22 '14 at 04:13
  • Out of curiosity, how serious of a test is it? Because I can't emphasize enough how easy it would be to mess with any client-only timer. You don't even need to be tech-savvy; just changing your system time and reloading the page would be enough to bypass most client-only solutions. – Semicolon Oct 22 '14 at 04:21
  • Yeah, I'm thinking about server side timeout, can you guide me to some tutorials, thanks for your interst and help – user1934044 Oct 22 '14 at 05:17
  • Well, the implementation details are pretty wide open; I don't even know what language your server code is in. I think the first question, if the integrity of the test is important, is actually about how you plan to track the users themselves. Accounts? Log-in and issue a session cookie? That's the obvious route, but the tools you would use to do that vary depending on the server technology you're using. Whatever solution you choose, you need "users" to attach the data to -- their timer and the test answers they submit. – Semicolon Oct 22 '14 at 05:44
  • I'm using meteor and using accounts.I'll find out, thanks for the help – user1934044 Oct 22 '14 at 05:47