Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a timer to a page 1

Status
Not open for further replies.

neillovell

Programmer
Aug 27, 2002
560
GB
Hi,
I've created a data access page from MS Access and unfrotunately the VBA code and buttons don't work. In particular, I had a timer. You pushed START and it displayed the current time in a text box. STOP displayed the current time (obviously you hit it when you finish your activity) in a second text box and a third text box displayed the difference between them, giving you the amount of time spent doing whatever.

How can I achieve this in HTML? Or DHTML I assume.
 
Here is an example for you :
Code:
<Html>
<head>
<Script language=&quot;Jscript&quot;>
var oInterval;

function RemindMsg(msg) {
  oInterval=window.setInterval(&quot;ShowDelayedMsg('&quot; + msg + &quot;')&quot;, 2000);
  // function stopTimer() will be called every 2 seconds
  // Note that you can add any parameters to the function call.
}

function ShowDelayedMsg(msg) {
  var oDiv=document.getElementById(&quot;container&quot;);
  var oldContent = oDiv.innerHTML;
  var curDate=new Date();
  oDiv.innerHTML = oldContent + curDate + &quot; : &quot; + msg + &quot;<BR>&quot;;
}

function stopTimer() {
  window.clearInterval(oInterval);
  // the timer is now stopped
}
</script>
</head>
<body>
  <input type=&quot;button&quot; value=&quot;remind me&quot; onclick=&quot;RemindMsg('another message');&quot;/>
  <input type=&quot;button&quot; value=&quot;stop timer&quot; onclick=&quot;stopTimer();&quot;/>
  <div id=&quot;container&quot;></div>
</body>
</html>
Water is not bad as long as it stays out human body ;-)
 
Wow that's great. The only problem is that when I hit Stop Timer I get a script error (object expected) and I can't fix it. I tried oInterval = window.clearInterval but that didn't work.
 
did you use the code as is ?
if no, the error should come from the oInterval declaration : it [g]MUST[/g] be declared [g]OUTSIDE[/g] any function in order to give it a global scope for the page. Water is not bad as long as it stays out human body ;-)
 
sorry for the wrong TGML tags in my previous post. They should have been [ b ] and [ /b ] Water is not bad as long as it stays out human body ;-)
 
Ah my fault, I renamed the function StopTimer()
and called it using stopTimer()

Schoolboy error there!

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top