Adding a Timer

People like to see how they are doing on a game compared to others. One way we may be able to provide some type of metric is by calculating the amount of time a given puzzle takes to solve. Doing so is reasonably straightforward in JavaScript. Essentially, we want to store the time the game starts and set another variable for when the game is completed. A few small changes to the javascripts/puzzle.js will make this happen.

First, we need to define some variables to hold when the game was started, when it will end, and the total time taken (see Listing 11–6).

Listing 11–6. Adding a Timer

var start, stopped, totalTime; function startTimer() {   start = new Date().getTime(); } function stopTimer() {   stopped = new Date().getTime(); ...

Get Beginning Facebook Game Apps Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.