-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.html
136 lines (107 loc) · 4.4 KB
/
timer.html
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Task timer</title>
<style>
tasklist {
display: grid;
grid-template-columns: repeat(4, 1fr);
}
task {
display: grid;
grid-template-columns: repeat(3, 1fr);
padding: 1rem;
margin: 0.5rem 0;
max-width: max(20vw, 400px);
}
</style>
</head>
<body>
<h1>Track your tasks</h1>
<form id="task-form" autocomplete="off">
<label for="name">New task </label>
<input type="text" name="name" id="task-name" required/>
<button type="submit" id="create-button">Create task</button>
</form>
<tasklist>
</tasklist>
<script>
const taskListElement = document.getElementsByTagName("tasklist")[0];
const taskForm = document.getElementById("task-form");
let taskList = [];
let tasksNumber = 0;
function createTask(taskObject) {
let taskElement = document.createElement("task");
let taskName = document.createElement("taskname");
let taskTimerDisplay = document.createElement("timerdisplay")
let taskTimerButton = document.createElement("button");
taskTimerDisplay.textContent = "00";
taskTimerDisplay.setAttribute("class", "timer")
taskTimerButton.textContent = "Timer";
let randomColor = Math.floor(Math.random()*16777215).toString(16);
console.log(randomColor)
taskElement.style.backgroundColor = `#${randomColor}`
// Add event listener to the taskTimerButton
taskTimerButton.addEventListener("click", function() {
if (taskElement.dataset.counting == "true") {
console.log("stop")
taskElement.setAttribute("data-counting", "false")
} else {
taskElement.setAttribute("data-counting", "true")
startTimer(taskElement);
}
});
taskName.textContent = taskObject.name;
taskElement.setAttribute("data-timer", 0)
taskElement.setAttribute("data-counting", "false")
taskElement.setAttribute("id", `task-${tasksNumber}`)
tasksNumber ++
taskElement.appendChild(taskName)
taskElement.appendChild(taskTimerDisplay);
taskElement.appendChild(taskTimerButton);
taskListElement.appendChild(taskElement)
}
taskForm.addEventListener('submit', function(event) {
event.preventDefault(); // Prevent the default form submission
// Get form data
const formData = new FormData(taskForm);
const formDataObject = Object.fromEntries(formData.entries()); // Convert FormData to object
createTask(formDataObject);
taskForm.reset();
});
function startTimer(taskElement){
let seconds = taskElement.dataset.timer;
// Define the setTimeout function
function timerFunction() {
let timer = taskElement.querySelector(".timer");
timer.textContent = seconds;
taskElement.setAttribute("data-timer", seconds);
console.log(taskElement.dataset.counting)
if (taskElement.dataset.counting == "false") {
clearTimeout(timerID); // Stop the timer
} else {
// Continue the timer by calling setTimeout recursively
timerID = setTimeout(timerFunction, 1000);
seconds++;
}
}
// Start the timer
timerFunction();
};
function stopTimer(taskElement){
taskElement.setAttribute("data-counting", "false")
let seconds = taskElement.dataset.timer;
setTimeout(function(){
seconds++;
let timer = taskElement.querySelector(".timer");
timer.textContent = seconds;
taskElement.setAttribute("data-timer", seconds);
startTimer(taskElement)
},1000);
};
</script>
</body>
</html>