-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScript.js
More file actions
69 lines (58 loc) · 1.94 KB
/
JavaScript.js
File metadata and controls
69 lines (58 loc) · 1.94 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
/**
* Created by Kirill on 10-Apr-17.
*/
function timeSet(value) {
document.getElementById('timeId').innerHTML = 'You time: ' + value / 1000 + 's';
};
function randomVerticalPosition() {
return Math.floor(Math.random() * 200) + 'px';
}
function randomHorizontalPosition() {
return Math.floor(Math.random() * 500) + 'px';
}
function randomShape() {
if (Math.random() > 0.5) {
return '50%';
} else {
return '0%';
}
}
function randomSize() {
return Math.floor(Math.random() * 100 + 50) + 'px';
}
function randomColor() {
var colorDictionary = {0:'blue',1:'red',2:'yellow',3:'brown',4:'green'};
return colorDictionary[Math.floor(Math.random() * 5)]
}
function randomDelay() {
return Math.floor(Math.random() * 900 + 100);
}
var alreadyBest = 1000;
function bestTime(newTime) {
if (newTime < alreadyBest) {
document.getElementById('bestId').innerHTML = 'Your best time: ' + newTime / 1000 + 's';
alreadyBest = newTime;
}
}
var date = new Date();
var globalTime = date.getTime();
document.getElementById('moving').onclick = function () {
document.getElementById('moving').style.display = 'none';
var date = new Date();
var timeDifference = date.getTime() - globalTime;
bestTime(timeDifference);
timeSet(timeDifference);
setTimeout(afterTimer, randomDelay());
};
function afterTimer() {
document.getElementById('moving').style.display = 'block';
document.getElementById('moving').style.backgroundColor = randomColor();
document.getElementById('moving').style.borderRadius = randomShape();
document.getElementById('moving').style.marginLeft = randomHorizontalPosition();
document.getElementById('moving').style.marginTop = randomVerticalPosition();
var size = randomSize();
document.getElementById('moving').style.height = size;
document.getElementById('moving').style.width = size;
var date = new Date();
globalTime = date.getTime();
}