应用ajax 来做个时间倒计时,并每隔一段时间提交一次到后台
1:用ajax 来做个时间倒计时,并每隔一分种提交一次。
var xmlHttpRequest;
var timer;
var saveTimer;
function createXMLHttpRequest() {//判断是否那种浏览器,并对不同浏览器进行创建xmlHttpRequest
try {
// Firefox, Opera 8.0+, Safari
xmlHttpRequest = new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
}
//发送请求函数
function sendRequestPost(url, param) {
createXMLHttpRequest();
xmlHttpRequest.open("POST", url, true);
xmlHttpRequest.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlHttpRequest.send(param);
}
function saveData() {
var article = document.getElementById("article").value;//获取id为article的变量的值
var funcID = document.getElementById("funcID").value;//获取id为funcID的变量的值
var dataID = document.getElementById("dataID").value;//获取id为dataID的变量的值
var id = document.getElementById("id").value;//获取id为id的变量的值
var url = "STUDone.do";//指定URL
var param = encodeURIComponent("article") + "="
+ encodeURIComponent(article) + "&"
+ encodeURIComponent("funcID") + "="
+ encodeURIComponent(funcID) + "&"
+ encodeURIComponent("dataID") + "="
+ encodeURIComponent(dataID) + "&" + encodeURIComponent("ID")
+ "=" + encodeURIComponent(id);
sendRequestPost(url, param);
}
function timingProg() {
var ss, mm;
var temp;
temp = document.getElementById('cdTime').innerHTML;
mm = Number(temp.substring(0, temp.indexOf(":")));
ss = Number(temp.substring(temp.indexOf(":") + 1));
//时间到
if (ss == 0 && mm == 0) {
clearInterval(timer);
clearInterval(saveTimer);
//time out !!!
document.getElementById('stu03Form').submit();
return;
}
//时间在减少
if (ss == 0) {
ss = 60;
}
ss = ss - 1;
if (ss == 59) {
mm = mm - 1;
}
//转换回字符串,补0
mm = String(mm);
ss = String(ss);
if (mm.length <= 1) {
mm = '0' + mm;
}
if (ss.length <= 1) {
ss = '0' + ss;
}
//输出到html
document.getElementById('cdTime').innerHTML = mm + ':' + ss;
}