We are currently displaying timestamp information in our ColdFusion webapp via the following JavaScript function. This function is called when a user clicks an HTML button on the web page. The problem is that the time is only current as of the last page load. So if the user is sitting on the page for a while, the time becomes inaccurate.
Is there a more elegant way to do this in JavaScript that is more accurate? Or is there a way in ColdFusion to pull the server clock information real-time?
HTML Button
<input class="timerStart" id="startButton2" type="button" value="Insert Start Time" onclick="insertAtCaret('messageBody','---Start Time:#DateFormat(Now())# : #TimeFormat(Now())#---');" />
Javascript Code
function insertAtCaret(areaID,text) {
var txtarea = $("textarea[data-focused=true]")[0];
if(txtarea!=undefined)
{
var scrollPos = txtarea.scrollTop;
var strPos = 0;
var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ?
"ff" : (document.selection ? "ie" : false ) );
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart ('character', -txtarea.value.length);
strPos = range.text.length;
}
else if (br == "ff") {strPos = txtarea.selectionStart;}
var front = (txtarea.value).substring(0,strPos);
var back = (txtarea.value).substring(strPos,txtarea.value.length);
txtarea.value=front+text+back; strPos = strPos + text.length;
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart ('character', -txtarea.value.length);
range.moveStart ('character', strPos);
range.moveEnd ('character', 0); range.select();
}
else if (br == "ff") {
txtarea.selectionStart = strPos;
txtarea.selectionEnd = strPos;
txtarea.focus();
}
txtarea.scrollTop = scrollPos;
}
}