Playgrounds
Snippets
Developers
Compiler
All
ActionScript
Ajax
AngularJS
Apache
AppleScript
ASP.NET
Bash
C
C#
C++
Coffee
CoffeeScript
ColdFusion
Command
CSS
Delphi
Django
ES6
GLSL
Grunt
Gulp
HAML
Haskell
HTML
iOS
Jade
Java
JavaScript
jQuery
JSX
Less
LUA
MDX
MySQL
Objective
Other
Pascal
Perl
PHP
Plain text
PowerShell
Processing
Progress
Prolog
Pseudocode
Python
Rails
RegExr
Ruby
SASS
Scala
Scheme
SCSS
SmallBASIC
Smarty
SQL
Stylus
SVG
Swift
TypeScript
VHDL
X++
XHTML
XML
Xojo
XSLT
New snippet
New playground
Sign up
Login
Upload
Playgrounds
Snippets
Developers
Sign up
Login
Back Button
JavaScript
by
iRuva
20th February 2012
<input type="button" value="Go Back" onclick="history.back(-1)" />
2 Responses
Reminds me of Get back by the Beatles. Good times.
1
Reply?
H
Hanon
8 years ago
<script>
class CountDown extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.targetDate = new Date(this.getAttribute('target-date'));
this.shadowRoot.innerHTML = `
<style>
</style>
<h1>CountDown</h1>
<div class="status"></div>
<span id="timer"></span>
`;
this.timer = this.shadowRoot.querySelector('#timer');
this.updateTimer();
this.interval = setInterval(() => this.updateTimer(), 1000);
}
disconnectedCallback() {
clearInterval(this.interval);
}
updateTimer() {
const now = new Date();
const diff = this.targetDate - now;
if (diff <= 0) {
this.timer.textContent = 'Done';
clearInterval(this.interval);
} else {
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
this.timer.textContent = `${days} days ${hours.toString().padStart(2, '0')}
:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')} left`;
}
}
}
customElements.define('count-down', CountDown);
</script>
0
Reply?
N
NO
1 year ago
Write a comment
Send Comment
You can use [html][/html], [css][/css], [php][/php] and
more
to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.
2 Responses
class CountDown extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.targetDate = new Date(this.getAttribute('target-date'));
this.shadowRoot.innerHTML = `
<style>
</style>
<h1>CountDown</h1>
<div class="status"></div>
<span id="timer"></span>
`;
this.timer = this.shadowRoot.querySelector('#timer');
this.updateTimer();
this.interval = setInterval(() => this.updateTimer(), 1000);
}
disconnectedCallback() {
clearInterval(this.interval);
}
updateTimer() {
const now = new Date();
const diff = this.targetDate - now;
if (diff <= 0) {
this.timer.textContent = 'Done';
clearInterval(this.interval);
} else {
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
this.timer.textContent = `${days} days ${hours.toString().padStart(2, '0')}
:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')} left`;
}
}
}
customElements.define('count-down', CountDown);
</script>
Write a comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.