09. Timers Intervals π©βπ«π§βπ«
index.html
<!DOCTYPE html>
<html lang="en-gb">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Timers and Intervals</title>
</head>
<body>
<h1 class="time" style="text-align:center;"></h1>
<div id="main"></div>
<script src="./script.js"></script>
</body>
</html>
script.js
// Selects element by class
var timeEl = document.querySelector(".time");
// Selects element by id
var mainEl = document.getElementById("main");
var secondsLeft = 10;
function setTime() {
// Sets interval in variable
var timerInterval = setInterval(function() {
secondsLeft--;
timeEl.textContent = secondsLeft + " seconds left till colorsplosion.";
if(secondsLeft === 0) {
// Stops execution of action at set interval
clearInterval(timerInterval);
// Calls function to create and append image
sendMessage();
}
}, 1000);
}
// Function to create and append colorsplosion image
function sendMessage() {
timeEl.textContent = " ";
var imgEl = document.createElement("img");
imgEl.setAttribute("src", "images/image_1.jpg");
mainEl.appendChild(imgEl);
}
setTime();