CMPUT 404

Web Applications and Architecture

JS Lab

Description

In this lab you will make a JS Clock/Timer/Stopwatch/Countdown "single-page" app. The app won't require any network traffic except loading a single HTML file. You will use DHTML, promises, and async/await to connect your code and make sure each piece of code runs at the same time, and that the Clock, Timer, Stopwatch, and Countdown functions all work at the same time, independently.

Warning!

Due to the Thanksgiving holiday, there will be no walkthroughs for this lab. You will submit a single HTML file on eClass. Please ensure your code is well-commented so the TAs know that you understand your code. See the Heroku lab description for detailed examples of good vs bad comments.

Learning Goals

Getting Started

  1. Create an HTML page: clock.html. This must contain all of your HTML, CSS and JS.

App

HTML

JS

You should put your JS in head in a single element. You can either have it specify a ES module, or you can start your JS with 'use strict';.

Make sure your JS doesn't try to do much before the page (and DOM) is finished loading! For example, you could create a main function and then have the browser call it when everything is loaded like this:

window.addEventListener('load', main);

Keep in mind in the above example, main is a reference (pointer) to a function or a method. You should never pass JS code as a string!

You should avoid using HTML attributes like onload or onclick to run JS code. Instead, use addEventListener like in the example above.

Ticker Class

Using the observer design pattern, and the singleton design pattern, create a JS model class that allows other classes to register to be notified once a second.

You can do this using a setTimeout in order to update the page once a second. For the purposes of this exercise you are not allowed to use setInterval. In addition, you should only use setTimeout once, and you must make sure that only one timeout is running at a time!

The ticker class needs to run approximately once per second to update the page. Do not update the page faster than twice per second, or slower than once every two seconds.

Your Ticker class should contain:

Hints:

The Page

The page should have a title and four distinct areas. One area for the clock, one for the timer, one for the stopwatch, and one for the countdown.

Use CSS to make each area separate.

Hints:

Clock

In the clock part of the page, there should be an appropriate element to show the current time.

Create a Clock class, to put all your JS code for managing the Clock. Make sure it has an update method and adds itself an as observer to the Ticker class.

Every time Ticker notifies the Clock, use JS to update the element in the DOM to show the current time.

Timer

Stopwatch

Countdown

Requirements

Restrictions

Violation of the restrictions will result a mark of zero.

Submission Instructions

Upload your entire app as a single HTML file directly to eClass.

Hints

For the countdown timer you can use an algorithm like this:

Imagine you have two datetimes a, and b. and a is less than b. Then we want to compute c = b - a

  1. c's seconds = b's seconds - a's seconds
  2. if c's seconds < 0, then borrow one minute from b, and increase c's seconds += 60
  3. c's minutes = b's minutes - a's minutes
  4. if c's minutes < 0, then borrow one hour from b, and increase c's minutes += 60
  5. and so on...

The tricky one is the days, where you need to calculate how many days you borrowed:

  1. c's days = b's days - a's days
  2. if c's days < 0, then borrow one month from b and increase c's days += X where X = the # of days between the original value of b and the value of b after you borrowed a month