-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtimecalculator.js
More file actions
62 lines (52 loc) · 1.99 KB
/
Copy pathtimecalculator.js
File metadata and controls
62 lines (52 loc) · 1.99 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
let timerInterval = null;
function startTimer() {
if (sessionStorage.getItem("timerRunning") === "true") return; // Timer zaten çalışıyorsa yeniden başlatma
const userEmail = sessionStorage.getItem("userEmail");
if (!userEmail) return; // Kullanıcı giriş yapmamışsa Timer çalışmasın
sessionStorage.setItem("timerRunning", "true"); // Timer çalışıyor olarak işaretle
timerInterval = setInterval(() => {
updateStudiedTime(1); // Her dakika sadece "1" dakika ekle
}, 60000); // Her dakika çalıştır
}
function stopTimer() {
if (timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
sessionStorage.removeItem("timerRunning"); // Timer çalışmıyor olarak işaretle
}
}
function updateStudiedTime(minutes) {
const userEmail = sessionStorage.getItem("userEmail");
if (!userEmail) {
stopTimer(); // Kullanıcı oturumu kapatmışsa Timer'ı durdur
return;
}
fetch("/update-studied-time", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email: userEmail, minutes }),
})
.then((response) => response.json())
.then((data) => {
if (!data.success) {
console.error("StudiedTime güncellenirken hata oluştu:", data.message);
}
})
.catch((error) => console.error("Sunucu hatası:", error));
}
// Sayfa yüklendiğinde Timer kontrolü
document.addEventListener("DOMContentLoaded", () => {
const userEmail = sessionStorage.getItem("userEmail");
if (userEmail) {
startTimer(); // Kullanıcı giriş yapmışsa Timer'ı başlat
} else {
stopTimer(); // Kullanıcı çıkış yapmışsa Timer'ı durdur
}
// Tarayıcı kapanışında süreyi kaydet
window.addEventListener("beforeunload", () => {
stopTimer();
if (studiedMinutes > 0) {
updateStudiedTime(studiedMinutes);
}
});
});