user3619463
user3619463

Reputation: 33

How to make countdown time not back to default time if page refresh

So, i have small quiz with timer mode. I set 2 minutes to answer my question but after 1 minutes user refresh the page, timer back to 2 minutes

I follow countdown library http://keith-wood.name/countdown.html

var def = 2 * 60 * 1000;
var now = new Date();
var timer = new Date(now.getTime() + def);
$('#countdown').countdown({until: timer});

I need to record 1 minutes left, maybe using cookie https://github.com/carhartl/jquery-cookie is best way ? but how ? somebody can give me example code ?

Upvotes: 0

Views: 371

Answers (1)

Andrew
Andrew

Reputation: 20121

I think a cookie would work best for this. When a user starts the quiz, set a cookie with the value set to current server date/time.

Check for a cookie each time the page loads, and if it exists, compare the cookie value to the current time to get the time left on the quiz.

from http://www.w3schools.com/js/js_cookies.asp:

With JavaScript: a cookie can be created like this:

document.cookie="username=John Doe";

You can also add an expiry date (in UTC or GMT time). By default, the cookie is deleted when the browser is closed:

document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00
GMT";

With a path parameter, you can tell the browser what path the cookie belongs to. By default, the cookie belongs to the current page.

document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00
GMT; path=/";

or with a simple jquery plugin: How do I set/unset cookie with jQuery?

Upvotes: 1

Related Questions