-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
66 lines (56 loc) · 2.87 KB
/
script.js
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
window.onload = function() {
var title = document.title;
var countdownElement = document.getElementById('countdown');
var startButton = document.getElementById('startBtn');
var my_audio = document.getElementById("audio");
let countdown_name = "";
startButton.addEventListener('click', function() {
startButton.setAttribute("disabled", "disabled");
var durationInput = document.getElementById('duration');
let initialDuration = parseInt(durationInput.value, 10);
var duration = initialDuration * 60;
tmp_countdown_name = document.getElementById("countdown-name").value;
if(tmp_countdown_name)
countdown_name = tmp_countdown_name
end_countdown_string = "Countdown of " + initialDuration + " minutes finished"
if(countdown_name)
end_countdown_string = "Countdown \"" + countdown_name + "\" of " + initialDuration + " minutes finished"
if (!isNaN(duration) && duration > 0) {
var targetDate = new Date().getTime() + (duration * 1000);
var countdownInterval = setInterval(function() {
var currentDate = new Date().getTime();
var timeLeft = targetDate - currentDate;
// Calculate days, hours, minutes, and seconds
var days = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
var hours = Math.floor((timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((timeLeft % (1000 * 60)) / 1000);
// Display the countdown
let countdown_string = days + 'd ' + hours + 'h ' + minutes + 'm ' + seconds + 's';
countdownElement.innerHTML = countdown_string;
document.title = countdown_string;
if (timeLeft < 0) {
clearInterval(countdownInterval);
window.focus(); // Focus the browser tab
my_audio.currentTime = "0";
my_audio.play();
alert(end_countdown_string);
document.title = title;
countdownElement.innerHTML = end_countdown_string
startButton.removeAttribute("disabled");
}
}, 1000);
}else{
alert("Error : input value is incorrect.");
startButton.removeAttribute("disabled");
}
});
};
window.addEventListener('beforeunload', function(event) {
// Cancel the event (modern browsers)
event.preventDefault();
// Chrome requires returnValue to be set
event.returnValue = '';
// Prompt the user with a warning message
return 'Are you sure you want to leave this page?';
});