本文介绍了"心跳"记录仪qustion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我发布了恼人的弹出问题,作为解决记录别人的时间花费在页面上,普遍的共识是使用Ajax调用一个计时器,以返回到该用户是服务器报告仍然在页面上......(以下为code我凸轮了)。

ok, I posted a 'annoying popup' question, as a solution to 'logging' someone's time spent on a page, the general consensus was to use an ajax call on a timer to report back to the server that the user was still on the page... (below is the code I cam up with).

一个问题我已经是httRequest似乎缓存...埃维回报显示了相同的时间戳......

one issue i have is the httRequest seems to be cached... evey return shows the same "time stamp"...

<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>

在code为AJAX导通时间,interval.cfm    #now()#

The code for the "ajax-on-time-interval.cfm" #now()#

推荐答案

您可以逃脱制作一个完整的HEAD请求,而不是得到,如果你只是ping服务器,并不在乎什么回来。

You can get away with making a HEAD request instead of a full get if you are just pinging the server and do not care what comes back.

此外使用同步请求是一个非常糟糕的主意。这意味着,如果在连接​​到服务器是坏的任何原因,用户的浏览器会冻结,他们将无法做任何事。测试它通过添加一个长期的睡眠时间在你的服务器端code,并与网页游戏。你的用户将不能享受这种类型的平,所以将其交换异步。

Also using a synchronous request is a VERY BAD idea. That means if the connection to the server is bad for any reason, the user's browser will freeze and they will not be able to do anything. Test it out by adding a long sleep time on your serverside code and play with the page. Your user's will not enjoy that type of ping so swap it for asynchronous.

要停止缓存,应设置正确的头在你的服务器端code开始。如果您没有设置正确,浏览器会缓存页面。这就是get请求是为了做,使你的浏览更加高效。

To stop the caching, you should set the right headers on your serverside code to begin with. If you do not set the correct, the browser will cache the page. That is what get requests are meant to do to make your browsing more efficient.

如果你想迫使它总是抢做最新的,最简单的就是追加改变查询字符串值。的Math.random()是一种常见的选择,而是一种更好的选择是新的Date()。的getTime(),因为它总是在同一台机器上的一个不同的值。

If you want to force it to always grab the newest, easiest thing to do is append a querystring value that changes. Math.random() is a common choice, but a better choice is new Date().getTime() since it is always a different value on the same machine.

"ajax-on-time-interval.cfm?ts=" + (new Date().getTime())

这篇关于&QUOT;心跳&QUOT;记录仪qustion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:48