An Abbreviated Introduction to JavaScript


Time and Timers

Abstract: An introduction to the use of time and timers in JavaScript program, with some emphasis on their use in educational applications, particularly quizzes.


Introduction

Often, we want something to happen to our pages after a period of time elapses. Some "timed changes" we might want include

In addition, we might want to use time in other ways, such as keeping track of how much time students spend on individual pages.

JavaScript provides a number of mechanisms for dealing with time, including a facility for scheduling actions and a date object (which is also used for times).

Scheduling Actions

Many of our uses of time relate to scheduling an action at a certain time, or after a particular amount of time has passed. JavaScript permits you to say something like "after N milliseconds have passed, do this".

The function for scheduling operations is setTimeout() and it has the form setTimeout(operation,delay). The delay is in milliseconds.

For example, if I want to bring up a dialog box after ten seconds, I'd use
setTimeout('alert("You may need some help")', 10000)

setTimeout returns a value that we can use at a later date. The primary use of that value is to cancel a scheduled operation, which you do with clearTimeout().

For example, when we load a page, we might start a timer for providing help. When the student clicks a button, we might want to cancel the help (and possibly reschedule it for a later time).

Repeating Actions

Because there are no limitations on the code that is executed, it is possible for the routine called by setTimeout() to schedule additional actions. This repeated use of setTimeout() is often employed in the creation of marquees, clocks, and such. As I've noted before, this is not the best design decision. Nonetheless, we'll experiment with it slightly.

Let's create a page in which the image changes every second. We can take advantage of our previously written selectFish routine and simply ensure that we call it every second. Basically, we'll add a helper routine which we call when the document loads. This routine updates the image and then schedules another update in one second.

// Function:
//   updateFish()
// Parameters:
//   (none)
// Description:
//   Updates the fish image and schedules another update in one second
function updateFish()
{
  selectFish();
  setTimeout("updateFish()", 1000);
} // updateFish()

You can view an example.

The Date Object

JavaScript provides a simple date object for dealing with times. You can obtain the current date/time with

<script language="javascript">
now = new Date();
</script>

The date object provides many methods. Of particular interest are

To determine how long the user spends on the page, you would create a new date object when you first load the page, create another when you leave the page, and compare the values their getTime() method returns.


Source text written by Samuel A. Rebelsky.

This page may be found at http://www.math.grin.edu/~rebelsky/Tutorials/JavaScript/Spring1999/time.html

Source text last modified Mon Apr 12 10:14:43 1999.

This page generated on Mon Apr 12 10:22:30 1999 by SiteWeaver. Validate this page's HTML.