j-p
j-p

Reputation: 3818

"heartbeat" logger qustion

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

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>

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

Upvotes: 1

Views: 697

Answers (3)

epascarello
epascarello

Reputation: 207501

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.

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.

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.

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

Upvotes: 1

wajiw
wajiw

Reputation: 12269

IE does cache Ajax requests. In order to stop that I normally just add a random variable to my request:

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); 
}

That should work as long as you aren't parsing the new 'random' variable on the back-end :-)

Upvotes: 1

benhowdle89
benhowdle89

Reputation: 37464

I think you can use

$.ajaxSetup({
cache: false
});

or its something like that...

Upvotes: -1

Related Questions