Use cookies to prevent refreshing online exam countdown JS Code
$ (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 the countdown function settime (remain Time) {var _ time = getCookieValue ("871d31bacfd4451484c5f70f8860c2a9"); var _ countdown = parseInt (getCookieValue ("$ {examinee. examineeId} ")/1000; if (_ countdown <= 0) {alert (" the exam time is up! "); EndExam ();} else {var _ second = _ countdown % 60; var _ minute = parseInt (_ countdown/60) % 60; var _ hour = parseInt (_ countdown/60)/60); if (_ hour <10) _ hour = "0" + _ hour. toString (); if (_ second <10) _ second = "0" + _ second. toString (); if (_ minute <10) _ minute = "0" + _ minute. toString (); remainTime.html (_ hour + ":" + _ minute + ":" + _ second); _ countdown --; editCookie ("$ {examin Ee. examineeId} ", _ countdown * 1000, _ countdown * 1000);} // setTimeout (function () {settime (remainTime) every 1000 milliseconds );}, 1000) ;}; // Add cookie function addCookie (name, value, expiresHours) {var cookieString = name + "=" + escape (value); // escape () the function can encode the string so that the string can be read on all computers. // Determine whether to set the expiration time. 0 indicates that the browser is disabled if (expiresHours> 0) {var date = new Date (); date. setTime (date. getTime () + expiresHours * 1000); cookieString = cookieString + "; expires =" + date. toUTCString ();} document. cookie = cookieString;} // modify the cookie value function editCookie (name, value, expiresHours) {var cookieString = name + "=" + escape (value ); if (expiresHours> 0) {var date = new Date (); date. setTime (date. getTime () + expiresHours * 1000); // The unit is milliseconds cookieString = cookieString + "; expires =" + date. toGMTString ();} document. cookie = cookieString;} // obtain the cookie value based on the name function getCookieValue (name) {var strCookie = document. cookie; 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 ;};};}