-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (59 loc) · 2.17 KB
/
script.js
File metadata and controls
71 lines (59 loc) · 2.17 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
// Global task array
const tasks = [];
// Function to add a new task
function addTask(title, dueTime, priority) {
const task = {
title,
dueTime: Date.now() + dueTime * 60000, // Convert minutes to milliseconds
priority,
};
tasks.push(task);
console.log("Task added:", task);
scheduleReminder(task); // Schedule a reminder for the task
}
// Function to display tasks
function displayTasks() {
const taskList = document.getElementById("taskList");
taskList.innerHTML = ""; // Clear existing tasks
if (tasks.length === 0) {
taskList.innerHTML = "<li>No tasks available</li>";
return;
}
tasks
.sort((a, b) => a.priority - b.priority) // Sort tasks by priority
.forEach(task => {
const listItem = document.createElement("li");
const timeRemaining = Math.max(0, Math.round((task.dueTime - Date.now()) / 60000)); // Minutes remaining
listItem.innerHTML = `
<span>${task.title} - Due in ${timeRemaining} mins</span>
<span>Priority: ${task.priority}</span>
`;
taskList.appendChild(listItem);
});
}
// Function to schedule a reminder
function scheduleReminder(task) {
const delay = task.dueTime - Date.now();
if (delay > 0) {
setTimeout(() => {
alert(`Reminder: "${task.title}" is due!`);
}, delay);
}
}
// Form submission handler
document.getElementById("taskForm").addEventListener("submit", function (e) {
e.preventDefault();
const title = document.getElementById("taskTitle").value;
const dueTime = parseInt(document.getElementById("taskDueTime").value, 10);
const priority = parseInt(document.getElementById("taskPriority").value, 10);
if (!title || isNaN(dueTime) || isNaN(priority)) {
alert("Please fill out all fields correctly!");
return;
}
addTask(title, dueTime, priority); // Add the new task
displayTasks(); // Update the task list
// Clear the form fields
document.getElementById("taskForm").reset();
});
// Initial display of tasks
displayTasks();