im doing a little test to check whether the user is “away” (unactive) or not;
function away_status(){
$("#away_stat_update").everyTime(1000, function(i) {
var number = Number($(this).html());
if( number == 20 ) {
// set user as away
alert( "user away" );
}
if( number > 20 ) {
$("*").mousemove(function(e){
$("#away_stat_update").html(0);
var number = Number(0);
// reset user to being online
alert( "user back online" );
});
}
$("#away_stat_update").html( number + 1 );
});
$("*").mousemove(function(e){
$("#away_stat_update").html(0);
});
}
away_status();
the only problem is that when the number is greater than 20 and the mouse is moved it keeps alerting “user back on line” instead of doing it just once. The number is resetting by the way.
I’m answering this in a slightly different way – as I think it’s always nice to solve a problem gracefully and with the briefest possible code.
This example does exactly what you want, fully working. It doesn’t write the number into the DOM, it holds it in memory in JavaScript – you can write it into the element if you wish to display the number, but this saves having to retrieve it each time.
The alerts also work as you described.