今天因为修改网站遇到了一些问题,需要用js来解决。所以也让自己学习了一些东西,现在把从网上找到的东西整理一下:
window.setInterval()
功能:按照指定的周期(以毫秒计)来调用函数或计算表达式。
语法:setInterval(code,millisec)
参数: code:在定时时间到时要执行的JavaScript代码串(可以是函数)。
millisec:设定的定时时间,用毫秒数表示。
返回值:定时器的ID值,可用于clearInterval()方法停止指定的定时器。
注:setInterval()方法会不停地调用函数,直到用clearInterval()终止定时或窗口被关闭。
window.clearInterval()
功能:取消由setInterval()方法设置的定时器。
语法:clearInterval(id_of_setinterval)
参数:id_of_setinterval:由setInterval()返回的ID值。该值标识了一个setInterval定时器。
也就是:window.setInterval()返回的就是window.clearInterval的参数
unction dibutu(){
var width = document.getElementsByTagName('body')[0].clientWidth;
document.getElementById("yidiv").style.width=width;
document.getElementById("yiimg").width=width;}
window.setInterval("dibutu()",1);
jQuery.metadata.setType("attr","data");
var metadata=jQuery("#persontest_info").metadata();
var total_time = parseInt(metadata.total_time);
var times = total_time * 60;
var timeup = false;
var syncTimer = function() {
var sync_url = persontest_info_sync_url;
var data = {};
data.jstime = times;
data.sheet_id = $('#sheetid').val();
$.ajax({
type:"GET",
url: sync_url,
data:data,
dataType: "json",
success:function(data) {
if (data) {
times = data;
}
}
});
};
var timeObj = window.setInterval(function(){
if (times <= 0) {
persontestSubmit();
window.clearInterval(timeObj);
return;
}
// time text turn red!
if (times <= 600 && timeup == false) {
timeup = true;
jQuery("#timeleft").addClass("timeup");
}
// sync time every 5 minute
if(times%300 == 0) {
syncTimer();
}
var text = Math.floor(times/60)+"\u5206"+times%60+"\u79d2";
$('#timeleft').html(text);
times --;
}, 1000);
// persontest ajax submit
function persontestSubmit() {
$("#persontestform").ajaxSubmit({
dataType: "json",
beforeSubmit: function(data, form, options) {
if (!showLoading("#persontestform", options, null, {button:true}))
return false;
$("#errormsg").hide();
},
success: function(data) {
try {
var retmsg = data;
if (retmsg.error) {
$("#errormsg").jobUtil("showError", retmsg.error);
}
if (retmsg.success) {
$("#errormsg").jobUtil("showSuccess", '\u63d0\u4ea4\u6210\u529f').fadeIn('fast');
window.location = retmsg.redirect;
}
} catch(e) {
return false;
}
}
});
}