What about this? I think it's much more useful to store the time rather than counting the time yourself.
I also implemented a workaround for a possible problem: when the AFK message is sent, it'll trigger the OnEvent_ChatWndReceiveMessage event and thus launching an infinite loop of AFK messages!
js code:
var bAway = false;
var bOwnMessage = false;
var nCounterTime;
function OnEvent_ChatWndSendMessage( pChatWnd, sMessage ){
if ( sMessage === '/counter' ) {
if ( bAway === true ) {
bAway = false;
} else {
bAway = true;
nCounterTime = new Date().getTime();
}
return false;
}
bOwnMessage = true;
MsgPlus.AddTimer( 'OwnMessage', 1000);
}
function OnEvent_ChatWndReceiveMessage( pChatWnd, sMessage, sOrigin, nMessageKind ){
if ( bOwnMessage ) {
bOwnMessage = false;
MsgPlus.CancelTimer( 'OwnMessage' );
return;
}
if( bAway ) {
pChatWnd.SendMessage( 'I have been away from the computer for: '+parseTime( )+'.' );
}
}
function OnEvent_Timer( sTimerId ) {
if( sTimerId === "OwnMessage" ) {
bOwnMessage = false;
}
}
function parseTime( ){
var nSeconds = Math.floor( ( new Date().getTime() - nCounterTime ) / 1000 );
var s = nSeconds % 1000;
var m = Math.floor( nSeconds / 60 ) % 60;
var h = Math.floor( nSeconds / (60 * 60) );
return h+'hours, '+m+'minutes and '+s+' seconds';
}