JAVASCRIPT TIMER LOOP

The times in the frame on the right have been generated on the fly by a JavaScript loop. Click here to see the results of a similar loop in Java. Both Java and JavaScript apparently use the operating system tick as the standard for time keeping. A tick pulses at nominally 18.2 times per second on a PC-DOS operating system, and at nominally 60 times per second on a Mac no matter the speed of the processor.

The value that is returned by JavaScript, as with GW-BASIC and Q-BASIC on a PC running MS-DOS, is a long number in milliseconds. In the case of JavaScript, this is the number of milliseconds since the beginning of 1970. Newer versions of Java no longer use this method (Date class), so the Java version returns the local wall clock time from your machine with a decimalized resolution of milliseconds (although we know better!).

As in pretty much every higher level language, the timer values that are returned by Java and JavaScript suggest millisecond-level resolution. Unfortunately, many people believe that this is indeed the resolution that is used, but values are reported in decimalized (millisecond) form simply because fractional form would be a whole lot more confusing! The loop that generates the values below shows that the timer ticks at a beat with a period of around 55 milliseconds for PC (1/18.2 seconds), or a period of around 16.6 milliseconds (1/60 seconds) on a Mac.

Note that in the values printed in the frame on the right, the number stays the same, then jumps by an increment that is much greater than one digit. On a PC, this will be 50 milliseconds about half the time and 60 milliseconds about half the time - or 16 and 17 on a Mac. (On a PC running something other than Microsoft Windows 3.11/95/98, you might see something much different. E.g., the resolution is much better on the superior Linux operating system.)

How long the value stays the same suggests the speed of your computer - the one running your browser, not the server. A faster machine will loop more times before the timer value increments.

The code for the JavaScript loop on the right looks like this:

<SCRIPT language = "JavaScript">
<!--
  n=0;
  while (n<100) {
  var Stuff = new Date();
  var dateStuff = Stuff.getTime();
  document.write(n+1, " - ", dateStuff, "<BR>");
  n++;
}
//-->
</SCRIPT>

Click here to see the results of a similar loop in Java.