Online Quiz Countdown to prevent refreshes based on Jquery+cookie
$ (function () {var _minute = parseint ("${exampaper.papertime}");
var _expireshours = _minute * 60 * 1000;
if (!hassetcookie ()) {Addcookie ("${examinee.examineeid}", _expireshours, _expireshours);
} settime ($ ("#remainTime"));
});
function Hassetcookie () {var strcookie = Document.cookie;
var Arrcookie = Strcookie.split (";");
for (var i = 0; i < arrcookie.length i++) {var arr = arrcookie[i].split ("=");
if (arr[0] = = "${examinee.examineeid}") {return true;
}
};
return false;
//Start Countdown function SetTime (remaintime) {var _time = Getcookievalue ("871d31bacfd4451484c5f70f8860c2a9");
var _countdown = parseint (Getcookievalue ("${examinee.examineeid}"))/1000; if (_countdown <= 0) {alert ("Time to test!")
");
Endexam ();
else {var _second = _countdown% 60; var _minute = parseint (_countdown/60)% 60;
var _hour = parseint (parseint (_COUNTDOWN/60)/60);
if (_hour <) _hour = "0" + _hour.tostring ();
if (_second <) _second = "0" + _second.tostring ();
if (_minute <) _minute = "0" + _minute.tostring ();
remaintime.html (_hour + ":" + _minute + ":" + _second);
_countdown--;
Editcookie ("${examinee.examineeid}", _countdown * 1000, _countdown * 1000);
settimeout (function () {settime (remaintime) is performed every 1000 milliseconds;
}, 1000);
}; Add Cookie function Addcookie (name, value, expireshours) {var cookiestring = name + "=" + escape (value);//esc
The ape () function encodes the string so that it can be read on all computers.
Determines whether the expiration time is set, 0 means that the invalidation if (expireshours > 0) {var date = new Date () is turned off by the browser.
Date.settime (Date.gettime () + expireshours * 1000);
cookiestring = cookiestring + "; expires=" + date.toutcstring (); } Document.cookie = CookiestRing //Modify the value of the cookie function Editcookie (name, value, expireshours) {var cookiestring = name + "=" + Escape (val
UE);
if (expireshours > 0) {var date = new Date (); Date.settime (Date.gettime () + expireshours * 1000);
Unit is millisecond cookiestring = cookiestring + "; expires=" + date.togmtstring ();
} document.cookie = cookiestring;
The value function Getcookievalue (name) {var Strcookie = Document.cookie) that gets the cookie according to the name;
var Arrcookie = Strcookie.split (";");
for (var i = 0; i < arrcookie.length i++) {var arr = arrcookie[i].split ("=");
if (arr[0] = = name) {return unescape (arr[1]);
Break
else {continue;
};
};
}
The above is the entire contents of this article, I hope you can enjoy.