Question

For this assignment, you will write a timer application in HTML and JavaScript. Your HTML document...

For this assignment, you will write a timer application in HTML and JavaScript.

Your HTML document should contain the following elements/features:

1. HTML tags:
a. An <input> tag labeled "Timer Duration" with the initial value 0
b. A <button> tag labeled "Start"
2. Script: When the user presses the button (1b), a function will begin that does the following:
a. Reads the value from the input field (1a)
b. Removes the <input> and <button> tags (1a & 1b)
c. Creates a new <p> tag, initialized to show the input value
d. Starts a timer that ticks down to zero. For every second that elapses, the paragraph tag (2c) will show the updated timer value (i.e., one less)
e. When the timer reaches zero, the countdown will stop and the paragraph tag (2c) will be removed and be replaced by a <button> tag labeled "New Timer"
f. When the <button> tag (2e) is pressed, it will be removed and the <input> and <button> tags (1a, 1b) will be recreated with their original formats

Homework Answers

Answer #1

<html>
<head>
<title>
Timer
</title>
</head>
<body>
<label for="seconds">
<input type="number" id="seconds" value="0"/>
</label>
  
<button id="start">
Start
</button>
  
<script>
var button;
function addButtonToListenClick() {
// Taking reference to start button
button = document.getElementById("start");

// adding an event to button so when button clicked a function handleStartClick called
button.addEventListener("click", handleStartClick);
}

addButtonToListenClick();
  

// in this function we remove tag start timer and create tag
function handleStartClick() {

// taking reference to input
   let input = document.getElementById("seconds");
// reading input value and converting it to number
let seconds = Number(input.value);
  
// remove input tag
input.remove();
  
// remove start button
button.remove()
  
// creating new p tag for timer
let pTag = document.createElement('p');

// giving paragraph an id
pTag.setAttribute("id", "timer");
  
// writing initial start time of time then we decrease it every second upto 0
pTag.innerHTML = seconds;

// adding our timer paragraph to body
document.body.appendChild(pTag);
  
/**
* starting timer
*/
// we user interval to stop timer on 0. It stores reference to our interval
let interval = null;

// setInterval calls function ( as first parameter) at every 1 second as described by second parameter 1000 ( ms ).
interval = setInterval(
   function() {
// decreasing time on every call
seconds -= 1;
  
// showing latest time in paragraph
       pTag.innerHTML = seconds;

// stop timer on reaching 0
if(seconds <= 0) {
clearInterval(interval)
// Recreating input tag and button

pTag.remove();

let inputTag = document.createElement('input');
// giving input same id timer
inputTag.setAttribute("id", "seconds");
inputTag.setAttribute("type", "number");
inputTag.value = 0;
document.body.appendChild(inputTag);

let buttonTag = document.createElement('button');

// giving button same id start
buttonTag.setAttribute("id", "start");
buttonTag.setAttribute("type", "button");
buttonTag.innerHTML = "New Timer";
document.body.appendChild(buttonTag);

addButtonToListenClick();
}
}, 1000
)
}
</script>
</body>
</html>

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions