防止刷新过快

1.定义COOKIE 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var Cookie =
{
get : function(name) {
var value = "";
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) { value = unescape(arr[2]); }
return value;
},
set : function(name,value,days){
if(isNaN(days)){
document.cookie = name+"="+escape(value)+";";
}else{
document.cookie = name+"="+escape(value)+";expires="+new Date(new Date().getTime()+days*86400000).toGMTString();
}
},
del : function(name){
document.cookie = name+"=;expires="+new Date().toGMTString();
}
}

2屏蔽刷新 检测是否刷新过快

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
document.onkeydown = function() {  
var k = window.event.keyCode;
if (k == 116 || (window.event.ctrlKey && window.event.keyCode==82)) //屏蔽 F5 刷新键 和CTRL+R
{
window.event.keyCode = 0;
window.event.returnValue = false;
chkrefresh();
}
}
var refreshtime = 5000;//5秒
function chkrefresh()//检测是否刷新过快
{
var oldtime = Cookie.get('refreshtime');
var nowtime = new Date().getTime();
oldtime = parseInt(oldtime);
if(oldtime+refreshtime<nowtime) {
window.location.reload();
Cookie.set('refreshtime',nowtime);
} else {
alert("刷新过快,请稍候");
}
}

防止刷新过快
https://huangzunxue998.top/2023/03/09/防止刷新过快/
Author
黄dada
Posted on
March 9, 2023
Licensed under