gpt4 book ai didi

javascript - "heartbeat"记录器问题

转载 作者:行者123 更新时间:2023-11-28 02:48:16 24 4
gpt4 key购买 nike

好吧,我发布了一个“烦人的弹出窗口”问题,作为“记录”某人在页面上花费的时间的解决方案,普遍的共识是在计时器上使用 ajax 调用来向服务器报告用户的情况仍在页面上...(下面是我想到的代码)。

我遇到的一个问题是 httRequest 似乎被缓存了...每个返回都显示相同的“时间戳”...

<script type="text/javascript">

var closeMe = 0;
var logMe = 0;

//the window doesn't have focus, do nothing or something that let's them know we're not logging at the moment
function onBlur() {
///stop the log interval
clearInterval ( logMe );
//after 2 min of non focus, close it.
closeMe = setInterval('window.close()',120000); //after 2 min of non focus, close it.
}

//the window has focus... keep logging.
function onFocus(){
//stop the close counter - in the event to 'blurred' sometime
clearInterval ( closeMe );
//run the AJAX on a schedule - we're doing it every minute - bu tyou can do it as often as you like
logMe = setInterval('logTime()',60000);
}

//call a script that logs another minute...
function logTime() {
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "ajax-on-time-interval.cfm", false);
xhReq.send(null);

var serverResponse = xhReq.responseText;
alert(serverResponse);
}

// check for Internet Explorer... IE uses 'onfocusin/out" - everything else uses "onfocus/blur"
if (/*@cc_on!@*/false) {
document.onfocusin = onFocus;
document.onfocusout = onBlur;
} else {
window.onfocus = onFocus;
window.onblur = onBlur;
}

</script>

“ajax-on-time-interval.cfm”的代码 #现在()#

最佳答案

IE 会缓存 Ajax 请求。为了阻止这种情况,我通常只需在我的请求中添加一个随机变量:

function logTime() {
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "ajax-on-time-interval.cfm?random="+Math.random(), false);
xhReq.send(null);

var serverResponse = xhReq.responseText;
alert(serverResponse);
}

只要您不在后端解析新的“随机”变量,这应该就可以工作:-)

关于javascript - "heartbeat"记录器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4403149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com