我想创建一种方法来跟踪用户访问网页的次数。
问题是,如果上次访问超过30分钟,我只想增加cookie访问次数。
我怎么能这样做?
现在这就是我必须设置并获取cookie:
getCookie('xVisitors');
setCookie('xVisitors', 1, 120);
function setCookie(b, c, e) {
var i = "";
e > 0 && (i = new Date, i.setTime(i.getTime() + e * 6E4), i = "; expires=" + i.toGMTString());
e = "";
document.cookie = b + "=" + c + i + "; path=/;" + e
};
function getCookie(b) {
for (var c = document.cookie.split(";"), e = 0; e < c.length; e++) {
for (var i = c[e]; i.charAt(0) == " ";) i = i.substring(1, i.length);
if (i.indexOf(b) == 0) return i.substring(b.length + 1, i.length);
}
return "";
}
答案 0 :(得分:0)
var cTime = new Date().getTime();
var numVisits = 1;
var xVisitors = getCookie('xVisitors').split('|');
if ( typeof xVisitors[1]!='undefined' ) {
numVisits = ( (cTime-parseInt(xVisitors[1])) > (30*60) ) ? (xVisitors[0]+1) : xVisitors[0];
}
setCookie('xVisitors',numVisits+"|"+cTime,120);