1

I am doing online Exam Management, I have set a time duration for an exam to 5 min.

When student attend this test, timer starts decreasing.

I made this timer using javascript.

What the problem is if the student refresh the page timer starts from first. How can I make the time to be static if page refreshed.

6 Answers 6

3

You cannot avoid the timer from resetting because when a page reload, all HTML and JS reloads. You can store the current time in a "cookie". Look up "Using cookies in Javascript".

The work flow would be as follows

  1. Page loads and you start a timer.
  2. User reloads the page.
  3. Listen for the onBeforeUnload (and onUnload) events in Javascript and write a cookie that stores the current time.
  4. When the page reloads back, read this time from the cookie and use that.
  5. When timer ends, be sure to clear the cookie.
Sign up to request clarification or add additional context in comments.

Comments

3

In short: Cookies or AJAX (PHP)

AJAX timer. Make a php page "remember" the time for the student and the javascript will tell the php to deduct the "second" or however long from the stored time.

Alternatively, try using a cookie.

1)Check if cookie exists

2) If not set the cookie to expire in 5 minutes from the start time. If yes, check if Cookie is expired.

3)If the cookie is expired, show "Time up" if not, keep waiting.

The problem is that Javascript can be run from the browser's address bar and as such is not really a good option in a secure test environment.

Comments

2

The only things in javascript that would stay around between pages or page refreshes are cookies.

You could periodically set the cookie value to be the time left and check it on page load and start the timer at that value if its not 0.

2 Comments

"he only things in javascript that would stay around between pages or page refreshes are cookies." - or AJAX values, essentially server-side cookies!
True, you could even use session if you're getting AJAX involved.
2

Make something like this in PHP:

<script type="text/javascript">
var remainingtime = <?php echo $remainingtime; /* remaining seconds */ ?>;
</script>

Then from JavaScript you could read the value of this variable.

EDIT: Later of course you have to check on the server side if the time is right, because the JavaScript value could be faked.

2 Comments

What happens if student hit refresh button in the browser. How can i assign value to php variable at that time?
you have to store the end/start time (even for the check at the end)... every time the user refreshes the page you recalc the remaining time.
1

You cant. However, you can do another thing, lets say less polite :

Use ajax to update the database saying that student X has already started the exam, and if it refreshes the page, read that value and blow his chances off ;)

Comments

1

Run the timer server-side and poll it using AJAX.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.