View difference between Paste ID: Hb6cBryL and BZLk10CD
SHOW: | | - or go back to the newest paste.
1
//Event Handler + other global variables
2
var bindEvt = (function () {
3
    "use strict";
4
    if (document.addEventListener) {
5
        return function (element, event, handler) {
6
            element.addEventListener(event, handler, false);
7
        };
8
    }
9
    return function (element, event, handler) {
10
        element.attachEvent('on' + event, handler);
11
    };
12
}()),
13
timeset,
14
countdown,
15
start,
16
pause,
17
reset;
18
19
//Convert seconds to time
20
function dhms(s, f) {
21
    var d = h = m = 0;
22
    switch (true) {
23
        case (s > 86400):
24
            d = Math.floor(s / 86400);
25
            s -= d * 86400;
26
        case (s > 3600):
27
            h = Math.floor(s / 3600);
28
            s -= h * 3600;
29
        case (s > 60):
30
            m = Math.floor(s / 60);
31
            s -= m * 60;
32
    }
33
    if (f != null) {
34
        var f = f.replace('dd', (d < 10) ? "0" + d : d);
35
        f = f.replace('d', d);
36
        f = f.replace('hh', (h < 10) ? "0" + h : h);
37
        f = f.replace('h', h);
38
        f = f.replace('mm', (m < 10) ? "0" + m : m);
39
        f = f.replace('m', m);
40
        f = f.replace('ss', (s < 10) ? "0" + s : s);
41
        f = f.replace('s', s);
42
    }
43
    else {
44
        f = d + ':' + h + ':' + m + ':' + s;
45
    }
46
    return f;
47
}
48
49
50
51
//set timer
52
function starttimer() {
53
    if (timeset == null) { timeset = document.getElementById('timerange').value * 60; };
54
    function startcounting() {
55
        document.getElementById('fulltime').innerHTML = dhms(timeset, 'hh:mm:ss');
56
        timeset -= 1;
57
    }
58
    startcounting();
59
    countdown = setInterval(startcounting, 1000);
60
    
61
}
62
function pausetimer() {
63
    window.clearInterval(countdown)
64
}
65
66
function resettimer() {
67
    window.clearInterval(countdown)
68
    timeset = null;
69
    document.getElementById('fulltime').innerHTML = "";
70
}
71
72
//detect interactions
73
function runtimer() {
74
    start = document.getElementById('start');
75
    pause = document.getElementById('pause');
76
    reset = document.getElementById('reset');
77
    bindEvt(start, "click", starttimer);
78
    bindEvt(pause, "click", pausetimer);
79
    bindEvt(reset, "click", resettimer);
80
}
81
82
//onload event
83
bindEvt(window, "load", runtimer);