Small HTML5 timer example

Source: Internet
Author: User

Comments: When you hear the timer, you may think it can only be implemented in js. In fact, this idea can be implemented even if you do not know html5, the following describes how to implement Timer In html5. If you are interested, do not miss html:

The Code is as follows:
<! DOCTYPE html>
<Html lang = "en">
<Head>
<Meta charset = "UTF-8">
<! -- Always force latest IE rendering engine (even in intranet) & Chrome Frame
Remove this if you use the. htaccess -->
<Meta http-equiv = "X-UA-Compatible" content = "IE = edge, chrome = 1">
<Title> HTML5 Timer for work-relax balance </title>
<Meta name = "description" content = "">
<Meta name = "author" content = "kevin">
<Meta name = "viewport" content = "width = device-width; initial-scale = 1.0">
<! -- Replace favicon. ico & apple-touch-icon.png in the root of your domain and delete these references -->
<Link rel = "shortcut icon" href = "/favicon. ico"/>
<Link rel = "apple-touch-icon" href = "/apple-touch-icon.png"/>
<Link rel = "stylesheet" type = "text/css" href = "css/style.css">
<Script>
CountDownSeconds = 60;
Var handle = null;
// Window load
Function onLoadWindow (){
ACanvas = document. getElementById ("countdownCanvas ");
Context = aCanvas. getContext ("2d ");
Var canvasText = "Press to Start ...";
Var xPos = aCanvas. width/2;
Var yPos = aCanvas. height/2;
Context. font = "12pt Century Gothic ";
Context. fillStyle = "#008000 ;";
Context. textAlign = "center ";
Context. textBaseline = "middle ";
Context. fillText (canvasText, xPos, yPos );
}
Function updateCanvas (theContext, width, height ){
If (countDownSeconds <0 ){
ClearInterval (handle );
Handle = null;
Alert ("hey, time is up! ");
Return 0;
}
MinStr = Math. floor (countDownSeconds/60 );
SecStr = countDownSeconds % 60;
If (minStr <10 ){
MinStr = "0" + minStr;
}
If (secStr <10 ){
SecStr = "0" + secStr;
}
Context. clearRect (0, 0, width, height );
Context. font = "24pt Century Gothic ";
Context. fillText (minStr + ":" + secStr, width/2, height/2 );
CountDownSeconds --;
}
Function startWorkCountDown (){
If (handle! = Null ){
ClearInterval (handle );
}
CountDownSeconds = document. getElementById ("workIntervalInput"). value * 60;
TimeDisplayCanvas = document. getElementById ("countdownCanvas ");
TimeDisplayContext2D = timeDisplayCanvas. getContext ("2d ");
UpdateCanvas (timeDisplayContext2D, timeDisplayCanvas. width, timeDisplayCanvas. height );
Handle = setInterval (function (){
UpdateCanvas (timeDisplayContext2D, timeDisplayCanvas. width, timeDisplayCanvas. height );
},1000 );
}
Function startRestCountDown (){
If (handle! = Null ){
ClearInterval (handle );
}
CountDownSeconds = document. getElementById ("restIntervalInput"). value * 60;
TimeDisplayCanvas = document. getElementById ("countdownCanvas ");
TimeDisplayContext2D = timeDisplayCanvas. getContext ("2d ");
UpdateCanvas (timeDisplayContext2D, timeDisplayCanvas. width, timeDisplayCanvas. height );
Handle = setInterval (function (){
UpdateCanvas (timeDisplayContext2D, timeDisplayCanvas. width, timeDisplayCanvas. height );
},1000 );
}
</Script>
</Head>
<Body onload = "onLoadWindow ()">
<Div align = "center">
<Header>
<H1> work-life balance timer </Header>
Please choose the work interval:
<Input name = "workIntervalInput" id = "workIntervalInput" type = "number" value = "25" min = "15" max = "45" step = "5"/>
Minutes

Please choose the rest interval:
<Input name = "restIntervalInput" id = "restIntervalInput" type = "number" value = "5" min = "3" max = "10" step = "1"/>
Minutes


<Canvas id = "countdownCanvas" width = "300" height = "50" style = "border: 2px solid black">
This is a canvas
</Canvas>


<Button onclick = "startWorkCountDown ()">
Work Hard
</Button>
<Button onclick = "startRestCountDown ()">
Take A Break
</Button>
<Footer>
<P>
& Copy; Copyright Reserved
</P>
</Footer>
</Div>
</Body>
</Html>

Css3:

The Code is as follows:
/*
* HTML5 ✰ Boilerplate
*
* What follows is the result of much research on cross-browser styling.
* Credit left inline and big thanks to Nicolas Gallagher, Jonathan Neal,
* Kroc Camen, and the H5BP dev community and team.
*
* Detailed information about this CSS: h5bp.com/css
*
* ==|=== Normalize ==================================== ======================================
*/

/* ===================================================== ========================================================
HTML5 display definitions
========================================================== ========================================= */
Article, aside, details, figcaption, figure, footer, header, hgroup, nav, section {display: block ;}
Header {text-shadow: #220000 0px 0px 10px ;}
Audio, canvas, video {display: inline-block; * display: inline; * zoom: 1 ;}
Audio: not ([controls]) {display: none ;}
[Hidden] {display: none ;}
/* ===================================================== ========================================================
Base
========================================================== ========================================= */
/*
* 1. Correct text resizing oddly in IE6/7 when body font-size is set using em units
* 2. Force vertical scrollbar in non-IE
* 3. Prevent iOS text size adjust on device orientation change, without disabling user zoom: h5bp.com/g
*/
Html {font-size: 100%; overflow-y: scroll;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100% ;}
Body {margin: 0; font-size: 24px; line-height: 1.231 ;}
Body, button, input, select, textarea {font-family: "Century Gothic"; color: #008000}
/*
* Remove text-shadow in selection highlight: h5bp.com/I
* These selection declarations have to be separate
* Also: hot pink! (Or customize the background color to match your design)
*/
:-Moz-selection {background: # fe57a1; color: # fff; text-shadow: none ;}
: Selection {background: # fe57a1; color: # fff; text-shadow: none ;}

/* ===================================================== ========================================================
Links
========================================================== ========================================= */
A {color: # 00e ;}
A: visited {color: # 551a8b ;}
A: hover {color: # 06e ;}
A: focus {outline: thin dotted ;}
/* Improve readability when focused and hovered in all browsers: h5bp.com/h */
A: hover, a: active {outline: 0 ;}

/* ===================================================== ========================================================
Typography
========================================================== ========================================= */
Abbr [title] {border-bottom: 1px dotted ;}
B, strong {font-weight: bold ;}
Blockquote {margin: 1em 40px ;}
Dfn {font-style: italic ;}
Hr {display: block; height: 1px; border: 0; border-top: 1px solid # ccc; margin: 1em 0; padding: 0 ;}
Ins {background: # ff9; color: #000; text-decoration: none ;}
Mark {background: # ff0; color: #000; font-style: italic; font-weight: bold ;}
/* Redeclare monospace font family: h5bp.com/j */
Pre, code, kbd, samp {font-family: monospace, monospace; _ font-family: 'courier new', monospace; font-size: 1em ;}
/* Improve readability of pre-formatted text in all browsers */
Pre {white-space: pre; white-space: pre-wrap; word-wrap: break-word ;}
Q {quotes: none ;}
Q: before, q: after {content: ""; content: none ;}
Small {font-size: 85% ;}
/* Position subscript and superscript content without affecting line-height: h5bp.com/k */
Sub, sup {font-size: 75%; line-height: 0; position: relative; vertical-align: baseline ;}
Sup {top:-0.5em ;}
Sub {bottom:-0.25em ;}

/* ===================================================== ========================================================
Lists
========================================================== ========================================= */
Ul, ol {margin: 1em 0; padding: 0 0 40px ;}
Dd {margin: 0 0 0 40px ;}
Nav ul, nav ol {list-style: none; list-style-image: none; margin: 0; padding: 0 ;}

/* ===================================================== ========================================================
Embedded content
========================================================== ========================================= */
/*
* 1. Improve image quality when scaled in IE7: h5bp.com/d
* 2. Remove the gap between images and borders on image containers: h5bp.com/e
*/
Img {border: 0;-ms-interpolation-mode: bicubic; vertical-align: middle ;}
/*
* Correct overflow not hidden in IE9
*/
Svg: not (: root) {overflow: hidden ;}

/* ===================================================== ========================================================
Figures
========================================================== ========================================= */
Figure {margin: 0 ;}

/* ===================================================== ========================================================
Forms
========================================================== ========================================= */
Form {margin: 0 ;}
Fieldset {border: 0; margin: 0; padding: 0 ;}
/* Indicate that 'label' will shift focus to the associated form element */
Label {cursor: pointer ;}
/*
* 1. Correct color not inheriting in IE6/24/60/9
* 2. Correct alignment displayed oddly in IE6/7
*/
Legend {border: 0; * margin-left:-7px; padding: 0 ;}
/*
* 1. Correct font-size not inheriting in all browsers
* 2. Remove margins in FF3/4 S5 Chrome
* 3. Define consistent vertical alignment display in all browsers
*/
Button, input, select, textarea {font-size: 100%; margin: 0; vertical-align: baseline; * vertical-align: middle ;}
/*
* 1. Define line-height as normal to match FF3/4 (set using! Important in the UA stylesheet)
* 2. Correct inner spacing displayed oddly in IE6/7
*/
Button, input {line-height: normal; * overflow: visible ;}
/*
* Reintroduce inner spacing in 'table' to avoid overlap and whitespace issues in IE6/7
*/
Table button, table input {* overflow: auto ;}
/*
* 1. Display hand cursor for clickable form elements
* 2. Allow styling of clickable form elements in iOS
*/
Button, input [type = "button"], input [type = "reset"], input [type = "submit"] {cursor: pointer;-webkit-appearance: button ;}
/*
* Consistent box sizing and appearance
*/
Input [type = "checkbox"], input [type = "radio"] {box-sizing: border-box ;}
Input [type = "search"] {-webkit-appearance: textfield;-moz-box-sizing: content-box;-webkit-box-sizing: content-box; box-sizing: content-box ;}
Input [type = "search"]:-webkit-search-decoration {-webkit-appearance: none ;}
/*
* Remove inner padding and border in FF3/4: h5bp.com/l
*/
Button:-moz-focus-inner, input:-moz-focus-inner {border: 0; padding: 0 ;}
/*
* 1. Remove default vertical scrollbar in IE6/24/60/9
* 2. Allow only vertical resizing
*/
Textarea {overflow: auto; vertical-align: top; resize: vertical ;}
/* Colors for form validity */
Input: valid, textarea: valid {}
Input: invalid, textarea: invalid {background-color: # f0dddd ;}

/* ===================================================== ========================================================
Tables
========================================================== ========================================= */
Table {border-collapse: collapse; border-spacing: 0 ;}
Td {vertical-align: top ;}

/* ==|== Primary styles ====================================== ======================================
Author:
========================================================== ========================================= */

/* ==|== Non-semantic helper classes ============================ ================
Please define your styles before this section.
========================================================== ========================================= */
/* For image replacement */
. Ir {display: block; border: 0; text-indent:-999em; overflow: hidden; background-color: transparent; background-repeat: no-repeat; text-align: left; direction: ltr ;}
. Ir br {display: none ;}
/* Hide from both screenreaders and browsers: h5bp.com/u */
. Hidden {display: none! Important; visibility: hidden ;}
/* Hide only comes ally, but have it available for screenreaders: h5bp.com/v */
. Visuallyhidden {border: 0; clip: rect (0 0 0); height: 1px; margin:-1px; overflow: hidden; padding: 0; position: absolute; width: 1px ;}
/* Extends the. visuallyhidden class to allow the element to be focusable when navigated to via the keyboard: h5bp.com/p */
. Visuallyhidden. focusable: active,. visuallyhidden. focusable: focus {clip: auto; height: auto; margin: 0; overflow: visible; position: static; width: auto ;}
/* Hide always ally and from screenreaders, but maintain layout */
. Invisible {visibility: hidden ;}
/* Contain floats: h5bp.com/q */
. Clearfix: before,. clearfix: after {content: ""; display: table ;}
. Clearfix: after {clear: both ;}
. Clearfix {zoom: 1 ;}

/* ==|== Media queries ============================================== ======================================
PLACEHOLDER Media Queries for Responsive Design.
These override the primary ('mobile first ') styles
Modify as content requires.
========================================================== ========================================= */
@ Media only screen and (min-width: 480px ){
/* Style adjustments for viewports 480px and over go here */
}
@ Media only screen and (min-width: 768px ){
/* Style adjustments for viewports 768px and over go here */
}

/* = | = Print styles = ======================================
Print styles.
Inlined to avoid required HTTP connection: h5bp.com/r
========================================================== ========================================= */
@ Media print {
* {Background: transparent! Important; color: black! Important; text-shadow: none! Important; filter: none! Important;-ms-filter: none! Important;}/* Black prints faster: h5bp.com/s */
A, a: visited {text-decoration: underline ;}
A [href]: after {content: "(" attr (href )")";}
Abbr [title]: after {content: "(" attr (title )")";}
. Ir a: after, a [href ^ = "javascript:"]: after, a [href ^ = "#"]: after {content :"";} /* Don't show links for images, or javascript/internal links */
Pre, blockquote {border: 1px solid #999; page-break-inside: avoid ;}
Thead {display: table-header-group;}/* h5bp.com/t */
Tr, img {page-break-inside: avoid ;}
Img {max-width: 100%! Important ;}
@ Page {margin: 0.5 ;}
P, h2, h3 {orphans: 3; widows: 3 ;}
H2, h3 {page-break-after: avoid ;}
}
# StartTimer {
Position: inherit
Width: 75px;
Height: 20px;
Top: 35px;
Left: 25px;
Cursor: pointer
}
# StopTimer {
Position: inherit;
Width: 75px;
Height: 20px;
Top: 10px;
Left: 25px;
Cursor: pointer
}
# CountdownCanvas {
Border-radius: 25px;
Box-shadow: 10px, 10px, 5px #888888;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.