-
Notifications
You must be signed in to change notification settings - Fork 28
Created Countdown Timer using JavaScript #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Manali71
wants to merge
4
commits into
craplab:main
Choose a base branch
from
Manali71:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Countdown Timer</title> | ||
| <link href='https://fonts.googleapis.com/css?family=Inconsolata' rel='stylesheet' type='text/css'> | ||
| <link rel="stylesheet" href="style.css"> | ||
| <style> | ||
| html { | ||
| box-sizing: border-box; | ||
| font-size: 10px; | ||
| background: #8E24AA; | ||
| background: linear-gradient(45deg, #42a5f5 0%,#478ed1 50%,#0d47a1 100%); | ||
| } | ||
|
|
||
| *, *:before, *:after { | ||
| box-sizing: inherit; | ||
| } | ||
|
|
||
| body { | ||
| margin: 0; | ||
| text-align: center; | ||
| font-family: 'Inconsolata', monospace; | ||
| } | ||
|
|
||
| .display__time-left { | ||
| font-weight: 100; | ||
| font-size: 20rem; | ||
| margin: 0; | ||
| color: white; | ||
| text-shadow: 4px 4px 0 rgba(0,0,0,0.05); | ||
| } | ||
|
|
||
| .timer { | ||
| display: flex; | ||
| min-height: 100vh; | ||
| flex-direction: column; | ||
| } | ||
|
|
||
| .timer__controls { | ||
| display: flex; | ||
| } | ||
|
|
||
| .timer__controls > * { | ||
| flex: 1; | ||
| } | ||
|
|
||
| .timer__controls form { | ||
| flex: 1; | ||
| display: flex; | ||
| } | ||
|
|
||
| .timer__controls input { | ||
| flex: 1; | ||
| border: 0; | ||
| padding: 2rem; | ||
| } | ||
|
|
||
| .timer__button { | ||
| background: none; | ||
| border: 0; | ||
| cursor: pointer; | ||
| color: white; | ||
| font-size: 2rem; | ||
| text-transform: uppercase; | ||
| background: rgba(0,0,0,0.1); | ||
| border-bottom: 3px solid rgba(0,0,0,0.2); | ||
| border-right: 1px solid rgba(0,0,0,0.2); | ||
| padding: 1rem; | ||
| font-family: 'Inconsolata', monospace; | ||
| } | ||
|
|
||
| .timer__button:hover, | ||
| .timer__button:focus { | ||
| background: rgba(0,0,0,0.2); | ||
| outline: 0; | ||
| } | ||
|
|
||
| .display { | ||
| flex: 1; | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| justify-content: center; | ||
| } | ||
|
|
||
| .display__end-time { | ||
| font-size: 4rem; | ||
| color: white; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div class="timer"> | ||
| <div class="timer__controls"> | ||
| <button data-time="20" class="timer__button">20 Secs</button> | ||
| <button data-time="300" class="timer__button">Work 5</button> | ||
| <button data-time="900" class="timer__button">Quick 15</button> | ||
| <button data-time="1200" class="timer__button">Snack 20</button> | ||
| <button data-time="3600" class="timer__button">Lunch Break</button> | ||
| <form name="customForm" id="custom"> | ||
| <input type="text" name="minutes" placeholder="Enter Minutes"> | ||
| </form> | ||
| </div> | ||
| <div class="display"> | ||
| <h1 class="display__time-left"></h1> | ||
| <p class="display__end-time"></p> | ||
| </div> | ||
| </div> | ||
|
|
||
| <script> | ||
| let countdown; | ||
| const timerDisplay = document.querySelector('.display__time-left'); | ||
| const endTime = document.querySelector('.display__end-time'); | ||
| const buttons = document.querySelectorAll('[data-time]'); | ||
|
|
||
| function timer(seconds) { | ||
| // clear any existing timers | ||
| clearInterval(countdown); | ||
|
|
||
| const now = Date.now(); | ||
| const then = now + seconds * 1000; | ||
| displayTimeLeft(seconds); | ||
| displayEndTime(then); | ||
|
|
||
| countdown = setInterval(() => { | ||
| const secondsLeft = Math.round((then - Date.now()) / 1000); | ||
| // check if we should stop it! | ||
| if(secondsLeft < 0) { | ||
| clearInterval(countdown); | ||
| return; | ||
| } | ||
| // display it | ||
| displayTimeLeft(secondsLeft); | ||
| }, 1000); | ||
| } | ||
|
|
||
| function displayTimeLeft(seconds) { | ||
| const minutes = Math.floor(seconds / 60); | ||
| const remainderSeconds = seconds % 60; | ||
| const display = `${minutes}:${remainderSeconds < 10 ? '0' : '' }${remainderSeconds}`; | ||
| document.title = display; | ||
| timerDisplay.textContent = display; | ||
| } | ||
|
|
||
| function displayEndTime(timestamp) { | ||
| const end = new Date(timestamp); | ||
| const hour = end.getHours(); | ||
| const adjustedHour = hour > 12 ? hour - 12 : hour; | ||
| const minutes = end.getMinutes(); | ||
| endTime.textContent = `Be Back At ${adjustedHour}:${minutes < 10 ? '0' : ''}${minutes}`; | ||
| } | ||
|
|
||
| function startTimer() { | ||
| const seconds = parseInt(this.dataset.time); | ||
| timer(seconds); | ||
| } | ||
|
|
||
| buttons.forEach(button => button.addEventListener('click', startTimer)); | ||
| document.customForm.addEventListener('submit', function(e) { | ||
| e.preventDefault(); | ||
| const mins = this.minutes.value; | ||
| console.log(mins); | ||
| timer(mins * 60); | ||
| this.reset(); | ||
| }); | ||
|
|
||
| </script> | ||
| </body> | ||
| </html> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Countdown Timer</title> | ||
| <link href='https://fonts.googleapis.com/css?family=Inconsolata' rel='stylesheet' type='text/css'> | ||
|
|
||
| <style> | ||
|
Manali71 marked this conversation as resolved.
|
||
| html { | ||
| box-sizing: border-box; | ||
| font-size: 10px; | ||
| background: #8E24AA; | ||
| background: linear-gradient(45deg, #42a5f5 0%,#478ed1 50%,#0d47a1 100%); | ||
| } | ||
|
|
||
| *, *:before, *:after { | ||
| box-sizing: inherit; | ||
| } | ||
|
|
||
| body { | ||
| margin: 0; | ||
| text-align: center; | ||
| font-family: 'Inconsolata', monospace; | ||
| } | ||
|
|
||
| .display__time-left { | ||
| font-weight: 100; | ||
| font-size: 20rem; | ||
| margin: 0; | ||
| color: white; | ||
| text-shadow: 4px 4px 0 rgba(0,0,0,0.05); | ||
| } | ||
|
|
||
| .timer { | ||
| display: flex; | ||
| min-height: 100vh; | ||
| flex-direction: column; | ||
| } | ||
|
|
||
| .timer__controls { | ||
| display: flex; | ||
| } | ||
|
|
||
| .timer__controls > * { | ||
| flex: 1; | ||
| } | ||
|
|
||
| .timer__controls form { | ||
| flex: 1; | ||
| display: flex; | ||
| } | ||
|
|
||
| .timer__controls input { | ||
| flex: 1; | ||
| border: 0; | ||
| padding: 2rem; | ||
| } | ||
|
|
||
| .timer__button { | ||
| background: none; | ||
| border: 0; | ||
| cursor: pointer; | ||
| color: white; | ||
| font-size: 2rem; | ||
| text-transform: uppercase; | ||
| background: rgba(0,0,0,0.1); | ||
| border-bottom: 3px solid rgba(0,0,0,0.2); | ||
| border-right: 1px solid rgba(0,0,0,0.2); | ||
| padding: 1rem; | ||
| font-family: 'Inconsolata', monospace; | ||
| } | ||
|
|
||
| .timer__button:hover, | ||
| .timer__button:focus { | ||
| background: rgba(0,0,0,0.2); | ||
| outline: 0; | ||
| } | ||
|
|
||
| .display { | ||
| flex: 1; | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| justify-content: center; | ||
| } | ||
|
|
||
| .display__end-time { | ||
| font-size: 4rem; | ||
| color: white; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div class="timer"> | ||
| <div class="timer__controls"> | ||
| <button data-time="20" class="timer__button">20 Secs</button> | ||
| <button data-time="300" class="timer__button">Work 5</button> | ||
| <button data-time="900" class="timer__button">Quick 15</button> | ||
| <button data-time="1200" class="timer__button">Snack 20</button> | ||
| <button data-time="3600" class="timer__button">Lunch Break</button> | ||
| <form name="customForm" id="custom"> | ||
| <input type="text" name="minutes" placeholder="Enter Minutes"> | ||
| </form> | ||
| </div> | ||
| <div class="display"> | ||
| <h1 class="display__time-left"></h1> | ||
| <p class="display__end-time"></p> | ||
| </div> | ||
| </div> | ||
|
|
||
| <script> | ||
| let countdown; | ||
| const timerDisplay = document.querySelector('.display__time-left'); | ||
| const endTime = document.querySelector('.display__end-time'); | ||
| const buttons = document.querySelectorAll('[data-time]'); | ||
|
|
||
| function timer(seconds) { | ||
| // clear any existing timers | ||
| clearInterval(countdown); | ||
|
|
||
| const now = Date.now(); | ||
| const then = now + seconds * 1000; | ||
| displayTimeLeft(seconds); | ||
| displayEndTime(then); | ||
|
|
||
| countdown = setInterval(() => { | ||
| const secondsLeft = Math.round((then - Date.now()) / 1000); | ||
| // check if we should stop it! | ||
| if(secondsLeft < 0) { | ||
| clearInterval(countdown); | ||
| return; | ||
| } | ||
| // display it | ||
| displayTimeLeft(secondsLeft); | ||
| }, 1000); | ||
| } | ||
|
|
||
| function displayTimeLeft(seconds) { | ||
| const minutes = Math.floor(seconds / 60); | ||
| const remainderSeconds = seconds % 60; | ||
| const display = `${minutes}:${remainderSeconds < 10 ? '0' : '' }${remainderSeconds}`; | ||
| document.title = display; | ||
| timerDisplay.textContent = display; | ||
| } | ||
|
|
||
| function displayEndTime(timestamp) { | ||
| const end = new Date(timestamp); | ||
| const hour = end.getHours(); | ||
| const adjustedHour = hour > 12 ? hour - 12 : hour; | ||
| const minutes = end.getMinutes(); | ||
| endTime.textContent = `Be Back At ${adjustedHour}:${minutes < 10 ? '0' : ''}${minutes}`; | ||
| } | ||
|
|
||
| function startTimer() { | ||
| const seconds = parseInt(this.dataset.time); | ||
| timer(seconds); | ||
| } | ||
|
|
||
| buttons.forEach(button => button.addEventListener('click', startTimer)); | ||
| document.customForm.addEventListener('submit', function(e) { | ||
| e.preventDefault(); | ||
| const mins = this.minutes.value; | ||
| console.log(mins); | ||
| timer(mins * 60); | ||
| this.reset(); | ||
| }); | ||
|
|
||
| </script> | ||
| </body> | ||
| </html> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to add some surprise element in the program? |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| JavaScript Countdown Timer: | ||
|
|
||
| This is a simple Javascript timer that counts down the time remiaining until a date/event for some default defined events. | ||
|
|
||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this file. @Manali71