之前做项目的时候也做过考试,但没有具体涉及过考试倒计时的内容,这次再次带着考试,真正的看了考试倒计时这块的内容,站在前人巨人的肩膀上,把这段内容总结下来:
首先看在view中添加的内容:
<span style="white-space:pre"> </span><span style="font-family:SimSun;"><tr>
<td colspan="2">
@*倒计时*@
考试倒计时:<div id="timer" style="text-align: center; font-size: 20px; width: 100%; float: right; height: 30px; color: rgba(0, 0, 0, 0.82);margin-bottom: 35px;"></div></td>
</tr></span>
<span style="font-family:SimSun;">@*倒计时*@
@{Html.RenderAction("GetExamTime", "StudentMain");}</span>
第二段代码引用的controller层中StudentMain类中的方法,在controller中的具体内容:
<span style="white-space:pre"> </span><span style="font-family:SimSun;">/// <summary>
/// 获取考试时间js代码
/// </summary>
/// <returns></returns>
public StringBuilder GetExamTime()
{
StringBuilder ExamTime = new StringBuilder();
ExamTime.Append("<script>");
ExamTime.Append("var maxtime = ");
//ExamTime.Append(Session["examTime"]);
ExamTime.Append("3600");
ExamTime.Append(";</script>");
return ExamTime;
}</span>
这样一来,界面上就显示出时间了,关键的问题所在就是要让时间出现倒计时的效果,接下来这段也就是考试倒计时的关键所在:
<span style="font-family:SimSun;font-size:18px;">function CountDown() {
//maxtime表示时间的秒数
if (maxtime >= 0) {
minutes = Math.floor(maxtime / 60);
seconds = Math.floor(maxtime % 60);
msg = minutes + ":" + seconds;
document.all["timer"].innerHTML = msg;
if (maxtime == 5 * 60) alert('还5分钟考试结束! ');
--maxtime;
}
else {
clearInterval(timer);
alert("考试时间到…………结束交卷");
//这里调用交卷方法
JumpPape()
}
}
timer = setInterval("CountDown()", 1000);</span>
嗯,仅需以上几段代码,考试倒计时的效果就出来啦,按秒倒计时的哦,哈哈,说难不难,说简单吧,小编还真着实琢磨了琢磨,功夫不负有心人,让我做出来了(虽然前人已经做得棒棒哒~),这也不知道节省了我多少时间呢,巨人给垫背,学习就有了更多的支持啦~~~