HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!DOCTYPE html>
<html>
<head>
<title>Timer</title>
</head>
<body>
<div class="container">
<div class="timmer">
<h1>Timer 1</h1>
<div>
<input id="ipt-1-h" class="ipt" type="number" placeholder="hour" value="0" />
<span>:</span>
<input id="ipt-1-m" class="ipt" type="number" placeholder="minute" value="0" />
<span>:</span>
<input id="ipt-1-s" class="ipt" type="number" placeholder="second" value="5" />
</div>
<div>
<input id="tmr-1-btn-start" class=" btn btn-start" type="button" value="Start"
onclick="btn_start_onclick(1)" />
<input id="tmr-1-btn-pause" disabled="true" class="btn btn-pause" type="button" value="Pause"
onclick="btn_pause_onclick(1)" />
<input id="tmr-1-btn-stop" disabled="true" class="btn btn-stop" type="button" value="Stop"
onclick="btn_stop_onclick(1)" />
</div>
</div>
<div class="timmer">
<h1>Timer 2</h1>
<div>
<input id="ipt-2-h" class="ipt" type="number" placeholder="hour" value="0" />
<span>:</span>
<input id="ipt-2-m" class="ipt" type="number" placeholder="minute" value="1" />
<span>:</span>
<input id="ipt-2-s" class="ipt" type="number" placeholder="second" value="5" />
</div>
<div>
<input id="tmr-2-btn-start" class="btn" type="button" value="Start" onclick="btn_start_onclick(2)" />
<input id="tmr-2-btn-pause" disabled="true" class="btn btn-pause" type="button" value="Pause"
onclick="btn_pause_onclick(2)" />
<input id="tmr-2-btn-stop" disabled="true" class="btn btn-stop" type="button" value="Stop"
onclick="btn_stop_onclick(2)" />
</div>
</div>
</div>
</body>
</html>
body {
background-color: #f0f0f0;
}
h1 {
color: #000000;
text-align: center;
}
p {
font-family: verdana;
font-size: 20px;
}
.container {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.timmer {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.ipt {
width: 50px;
height: 50px;
font-size: 20px;
text-align: center;
}
.btn {
margin: 10px;
width: 100px;
height: 50px;
font-size: 20px;
text-align: center;
}
class EventEmitter {
constructor() {
this._events = {};
}
on(type, listener) {
if (this._events[type]) {
this._events[type].push(listener);
} else {
this._events[type] = [listener];
}
}
emit(type, ...args) {
if (this._events[type]) {
this._events[type].forEach(listener => {
listener(...args);
});
}
}
removeListener(type, listener) {
if (this._events[type] && listener) {
this._events[type] = this._events[type].filter(l => l !== listener);
} else if (this._events[type] && !listener) {
this._events[type] = [];
}
}
}
class Timmer extends EventEmitter {
constructor() {
super();
this.name = 'undefined';
this.timmer = undefined;
this.h = 0;
this.m = 0;
this.s = 10;
}
_on_update() {
if (0 === this.h && 0 === this.m && 0 === this.s) {
this.stop();
return;
} else if (0 === this.s) {
this.s = 59;
if (0 === this.m) {
this.m = 59;
this.h = this.h - 1;
} else {
this.m = this.m - 1;
}
} else {
this.s = this.s - 1;
}
this.show()
// emit an event
this.emit('update', {
h: this.h,
m: this.m,
s: this.s
});
if (0 === this.h && 0 === this.m && 0 === this.s) {
this.stop();
}
}
start() {
if (this.timmer) {
console.log(`[${this.name}] started`);
return;
}
console.log(`[${this.name}] starts`);
this.timmer = setInterval(() => {
this._on_update();
}, 1000);
this.show();
// emit an event
this.emit('start', {
h: this.h,
m: this.m,
s: this.s
});
}
stop() {
console.log(`[${this.name}] stoped`);
clearInterval(this.timmer);
this.timmer = undefined;
// emit an event
this.emit('stop', {
h: this.h,
m: this.m,
s: this.s
});
}
pause() {
console.log(`[${this.name}] paused`);
clearInterval(this.timmer);
this.timmer = undefined;
// emit an event
this.emit('pause', {
h: this.h,
m: this.m,
s: this.s
});
}
show() {
console.log(`[${this.name}]current time: ${this.h}:${this.m}:${this.s}`);
}
}
const t1 = new Timmer();
t1.name = 'Timer 1';
const t2 = new Timmer();
t2.name = 'Timer 2';
const list_timmer = [t1, t2];
const list_sound = ['miao', 'wang'];
const list_sound_str = ['🐱meow~~~', '🐶woof~woof~woof~'];
function play_audio(sound) {
const audio = document.createElement('audio');
audio.src = `${sound}.mp3`;
audio.play();
}
function btn_start_onclick(i) {
// get the input value
const ipt_h = document.getElementById(`ipt-${i}-h`);
const ipt_m = document.getElementById(`ipt-${i}-m`);
const ipt_s = document.getElementById(`ipt-${i}-s`);
// set the state of input fields and buttons
dom_update_inputs(i, "COUNTING");
// take the corresponding timer from the timer array
const tmr = list_timmer[i - 1];
// assign the input value to the timer
tmr.h = Number(ipt_h.value);
tmr.m = Number(ipt_m.value);
tmr.s = Number(ipt_s.value);
// listen to timer's update event and synchronize the time to the page
tmr.removeListener('update');
tmr.removeListener('stop');
tmr.on('update', () => dom_update_timmer(i));
tmr.on('stop', () => {
console.log(list_sound_str[i - 1]);
});
tmr.on('stop', () => {
// play the sound
play_audio(list_sound[i - 1]);
// set the state of input fields and buttons
dom_update_inputs(i, "STOPPED");
});
// start the timer
tmr.start();
}
function btn_pause_onclick(i) {
dom_update_inputs(i, "PAUSED");
// take the corresponding timer from the timer array
const tmr = list_timmer[i - 1];
// pause the timer
tmr.pause();
}
function btn_stop_onclick(i) {
dom_update_inputs(i, "STOPED");
// take the corresponding timer from the timer array
const tmr = list_timmer[i - 1];
// stop the timer
tmr.stop();
}
function dom_update_inputs(i, status) {
if ('COUNTING' === status) {
// set the state of input fields
document.getElementById(`ipt-${i}-h`).disabled = true;
document.getElementById(`ipt-${i}-m`).disabled = true;
document.getElementById(`ipt-${i}-s`).disabled = true;
// set the state of buttons
document.getElementById(`tmr-${i}-btn-start`).disabled = true;
document.getElementById(`tmr-${i}-btn-pause`).disabled = false;
document.getElementById(`tmr-${i}-btn-stop`).disabled = false;
} else if ('PAUSED' === status) {
// set the state of input fields
document.getElementById(`ipt-${i}-h`).disabled = false;
document.getElementById(`ipt-${i}-m`).disabled = false;
document.getElementById(`ipt-${i}-s`).disabled = false;
// set the state of buttons
document.getElementById(`tmr-${i}-btn-start`).disabled = false;
document.getElementById(`tmr-${i}-btn-pause`).disabled = true;
document.getElementById(`tmr-${i}-btn-stop`).disabled = false;
} else if ('STOPPED' === status) {
// set the state of input fields
document.getElementById(`ipt-${i}-h`).disabled = false;
document.getElementById(`ipt-${i}-m`).disabled = false;
document.getElementById(`ipt-${i}-s`).disabled = false;
// set the state of buttons
document.getElementById(`tmr-${i}-btn-start`).disabled = false;
document.getElementById(`tmr-${i}-btn-pause`).disabled = true;
document.getElementById(`tmr-${i}-btn-stop`).disabled = true;
}
}
function dom_update_timmer(i) {
// take the corresponding timer from the timer array
const tmr = list_timmer[i - 1];
// show the time on the page
document.getElementById(`ipt-${i}-h`).value = tmr.h;
document.getElementById(`ipt-${i}-m`).value = tmr.m;
document.getElementById(`ipt-${i}-s`).value = tmr.s;
}
Also see: Tab Triggers