On the previous page we saw that we could use setTimeout() to make a function repeat on a prescribed interval. We can also do this using the setInterval method. The following example creates a web page seconds timer using this method.
Instead of writing a function and then using setTimeout on the function we can create a function that repeats and put our code for the things we want to do inside of it - one function instead of two.
Practice
7.1
Modify the example above to show minutes and seconds.
HINT
You will need to use modulus division and rounding down.
Modulus division returns the remainder of an uneven division.
Rounding down is done using the Math.floor() function.
The example below shows how to use modulus (%) and Math.floor(). Try it out.
<!DOCTYPE html>
<html>
<body>
<h2>The % Operator and Math.floor()</h2>
<p id="demo"></p>
<script>
var x = 7;
var y = 2;
var a = Math.floor(x / y);
var z = x % y;
document.getElementById("demo").innerHTML = x + " / " + y + " = " + a + " with " + z + " remainder.";
</script>
</body>
</html>