Preface
JavaScript countdown is not difficult to implement, but it is often recalculated once refreshed. What should I do if I want to do without recalculation?
There are several ideas,
1: Cookies
2: Local cache
3: window.name…
The first two are easier to understand. Today I will implement the refresh without recalculation for you using window.name . The code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>js Countdown-Refresh without recalculating</title></head> <body><SCRIPT LANGUAGE="JavaScript"><!--var maxtime;if(window.name==''){ maxtime = 1*60; }else{ maxtime = window.name;} function CountDown(){ if(maxtime>=0){ minutes = Math.floor(maxtime/60); seconds = Math.floor(maxtime%60); msg = "There are still "+ minutes +" minutes" + seconds + "seconds" before the end of the exam;// document.all["timer"].innerHTML = msg; document.getElementById("timer").innerHTML = msg; if(maxtime == 5*60) alert('Note, there are 5 minutes left!'); --maxtime; window.name = maxtime; } else{ clearInterval(timer); alert("Exam time is up, end!"); }}timer = setInterval("CountDown()",1000);//--></SCRIPT><div id="timer" style="color:red"></div> </body></html>Summarize
OK, that's how it's implemented, it's very simple! The above is all about JavaScript's countdown to refresh and not re-remember. This function is very practical in mock exams, and I hope it will be helpful to everyone.