forked from AdaGold/jQuery-Stopwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (66 loc) · 1.91 KB
/
index.js
File metadata and controls
78 lines (66 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function StopWatch(callback) {
this._time = 0
this._interval = null
this._delay= 1000
this._callback = callback
this.start = function (dilay) {
this._delay= 1000
if (this._interval){return}
this._interval = setInterval(this.addTime.bind(this), this._delay)
}
this.stop = function (){
clearInterval(this._interval)
this._interval = null
}
this.reset = function (){
this._delay= null
this._time = 0
}
this.addTime = function() {
this._time += this._delay
this._callback(this._time)
}
this.pad = function(num, size) {
var s = "0000" + num;
return s.substr(s.length - size);
}
}
// add code here
$(document).ready(function() {
console.log("Yay! Melissa!");
var container = $('#stopWatch')
var display = container.children('.display')
var buttons = container.children('button.stopWatch')
var stopWatch = new StopWatch(function(newTime) {
h = Math.floor( newTime / (60 * 60 * 1000) );
newTime = newTime % (60 * 60 * 1000);
m = Math.floor( newTime / (60 * 1000) );
newTime = newTime % (60 * 1000);
s = Math.floor( newTime / 1000 );
times = this.pad(h, 2) + ':' + this.pad(m, 2) + ':' + this.pad(s, 2);
display.text(times)
})
buttons.on('click', function(event) {
event.preventDefault()
//interations built in.
//it is good practice preventing default
//wich botton did I click
//and what should I do in response
console.log("you click me!");
var button = $(this)
if (button.hasClass('start')) {
//start the watch
stopWatch.start()
console.log("start watch");
} else if (button.hasClass('stop')) {
//stop the watch
console.log("stop watch");
stopWatch.stop();
} else {
console.log("reset watch");
stopWatch.reset();
}
// console.log(this); //the object you are interating with, in thiscase the button
// console.log(event); //Event Object
})
})