Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Event Handler + other global variables
- var bindEvt = (function () {
- "use strict";
- if (document.addEventListener) {
- return function (element, event, handler) {
- element.addEventListener(event, handler, false);
- };
- }
- return function (element, event, handler) {
- element.attachEvent('on' + event, handler);
- };
- }()),
- timeset,
- countdown,
- start,
- pause,
- reset;
- //Convert seconds to time
- function dhms(s, f) {
- var d = h = m = 0;
- switch (true) {
- case (s > 86400):
- d = Math.floor(s / 86400);
- s -= d * 86400;
- case (s > 3600):
- h = Math.floor(s / 3600);
- s -= h * 3600;
- case (s > 60):
- m = Math.floor(s / 60);
- s -= m * 60;
- }
- if (f != null) {
- var f = f.replace('dd', (d < 10) ? "0" + d : d);
- f = f.replace('d', d);
- f = f.replace('hh', (h < 10) ? "0" + h : h);
- f = f.replace('h', h);
- f = f.replace('mm', (m < 10) ? "0" + m : m);
- f = f.replace('m', m);
- f = f.replace('ss', (s < 10) ? "0" + s : s);
- f = f.replace('s', s);
- }
- else {
- f = d + ':' + h + ':' + m + ':' + s;
- }
- return f;
- }
- //set timer
- function starttimer() {
- if (timeset == null) { timeset = document.getElementById('timerange').value * 60; };
- function startcounting() {
- document.getElementById('fulltime').innerHTML = dhms(timeset, 'hh:mm:ss');
- timeset -= 1;
- }
- startcounting();
- countdown = setInterval(startcounting, 1000);
- }
- function pausetimer() {
- window.clearInterval(countdown)
- }
- function resettimer() {
- window.clearInterval(countdown)
- timeset = null;
- document.getElementById('fulltime').innerHTML = "";
- }
- //detect interactions
- function runtimer() {
- start = document.getElementById('start');
- pause = document.getElementById('pause');
- reset = document.getElementById('reset');
- bindEvt(start, "click", starttimer);
- bindEvt(pause, "click", pausetimer);
- bindEvt(reset, "click", resettimer);
- }
- //onload event
- bindEvt(window, "load", runtimer);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement