# Appendix: Various Features

We introduce some features that may be useful for the final project.

## Iteration by Timer Events

We have learned how to call a function by a timer in Exercise 06-10. In addition, we can call a function repeatedly without human interaction.

An interval timer invokes a JavaScript function repeatedly with a certain interval. To set the timer, execute the following:

• `variable = setInterval(function, interval);`

function is specified in the same way as the following of `onclick=`. interval is a number by the millisecond. An ID of the timer is stored into variable. This ID is required when we want to stop the timer.

To stop the timer, execute the following:

• `clearInterval(timerID);`

Below is a sample program. We assume an image file `picture.jpg` exists in the same directory.

• `interval.html`
• `interval.js`

funtion in `setInterval` can be specified with a function name alone. So, the 6th line above is equal to:
`timerId = setInterval(change, 100);`

## Random Number

Calculating a mathmematical formula always results the same value. However, in some computer program, we want different values each time we calculate. For example, when calculating enemy movement in a video game, random movement is more enjoyable than regular movement.

Random numbers are a sequence of numbers that are unpredictable. In JavaScript, a function `Math.random()` calculates (pseudo) random numbers. Executing `Math.random()` with console, you will see some nubmer which is different each time you execute.

The result of `Math.random()` is a decimal number no less than 0 and less than 1. In case we want to get a random element of an array, we need an integral random numbers. To convert decimal to integer, we use multiplication and a round-down function `Math.floor()`. For example, to get an integer between 0 and 9, execute the following:

• `Math.floor(Math.random()*10)`

Below is a smaple program to display one of rock, paper and scissors randomly.

• `random.html`
• `random.css`
• `random.js`
• We can rewrite `random.js` as follows: