I would like to create a 20 seconds countdown timer in my html. I have a table that will automatically refresh every 20 seconds, now what I want is to be able to see when will it refresh again. Is there a way to do this? Also, it will be much awesome if the timer is like a progress bar or something.
JavaScript
x
88
88
1
<script>
2
3
document.addEventListener("DOMContentLoaded",function(){
4
5
google.script.run.withSuccessHandler(generateTable).getOnline();
6
google.script.run.withSuccessHandler(generateTable1).getStatus();
7
8
setInterval(() => {
9
document.getElementById("tablebody").innerHTML = "";
10
document.getElementById("tablebody1").innerHTML = "";
11
google.script.run.withSuccessHandler(generateTable).getOnline();
12
google.script.run.withSuccessHandler(generateTable1).getStatus();
13
google.script.run.withSuccessHandler(getOnline).generateTable();
14
google.script.run.withSuccessHandler(getStatus).generateTable1();
15
}, 20000);
16
17
18
});
19
20
function generateTable(dataArray){
21
22
var tbody = document.getElementById("tablebody");
23
var tbody1 = document.getElementById("tablebody").innerHTML;
24
25
dataArray.forEach(function(r){
26
var row = document.createElement("tr");
27
var col1 = document.createElement("td");
28
col1.textContent = r[0];
29
var col2 = document.createElement("td");
30
col2.textContent = r[1];
31
var col3 = document.createElement("td");
32
col3.textContent = r[2];
33
var col4 = document.createElement("td");
34
col4.textContent = r[3]; // modified code
35
row.appendChild(col1);
36
row.appendChild(col2);
37
row.appendChild(col3);
38
row.appendChild(col4);
39
tbody.appendChild(row);
40
});
41
42
}
43
44
function generateTable1(dataArray){
45
46
var tbody = document.getElementById("tablebody1");
47
var tbody1 = document.getElementById("tablebody1").innerHTML;
48
49
dataArray.forEach(function(r){
50
var row = document.createElement("tr");
51
var col1 = document.createElement("td");
52
col1.textContent = r[0];
53
var col2 = document.createElement("td");
54
col2.textContent = r[1];
55
var col3 = document.createElement("td");
56
col3.textContent = r[2];
57
var col4 = document.createElement("td");
58
col4.textContent = r[3];
59
var col5 = document.createElement("td");
60
col5.textContent = r[4];
61
var col6 = document.createElement("td");
62
col6.textContent = r[5];
63
var col7 = document.createElement("td");
64
col7.textContent = r[6];
65
66
row.appendChild(col1);
67
row.appendChild(col2);
68
row.appendChild(col3);
69
row.appendChild(col4);
70
row.appendChild(col5);
71
row.appendChild(col6);
72
row.appendChild(col7);
73
tbody.appendChild(row);
74
});
75
76
}
77
78
var timeleft = 10;
79
var downloadTimer = setInterval(function(){
80
if(timeleft <= 0){
81
clearInterval(downloadTimer);
82
document.getElementById("countdown").innerHTML = "Finished";
83
} else {
84
document.getElementById("countdown").innerHTML = timeleft + " seconds remaining";
85
}
86
timeleft -= 1;
87
}, 1000);
88
</script>
JavaScript
1
27
27
1
<h1>🟢 Current Users</h1>
2
<p> The table will automatically refresh every 10 seconds to display new users </p>
3
<div id="countdown"></div>
4
<table>
5
<tr>
6
<th>Timestamp & Current TimeZone</th>
7
<th>Name</th>
8
<th>Last Name</th>
9
<th>EID</th>
10
</tr>
11
<tbody id="tablebody">
12
</table>
13
<hr class="rounded">
14
<h1>Assigned Case | Agent Status and Information</h1>
15
<p> The table will automatically refresh every 30 seconds to display new users </p>
16
<table>
17
<tr>
18
<th>EID</th>
19
<th>Name</th>
20
<th>Rest Day</th>
21
<th>Shift</th>
22
<th>ASSIGNED CASE</th>
23
<th>TM STATUS</th>
24
<th>WORK STATUS</th>
25
</tr>
26
<tbody id="tablebody1">
27
</table>
Advertisement
Answer
For a progress Bar with 20 second timer:
JavaScript
1
16
16
1
let time_left = 20;
2
var progress = document.getElementById("progress");
3
var timeleft = document.getElementById("timeleft");
4
setInterval(function() {
5
time_left = (time_left - 1);
6
timeleft.innerText = parseInt(time_left / 1) + "s Left";
7
progress.style.width = ((time_left / 20) * 100) + "%";
8
if (time_left <= 0) {
9
10
// Do what ever you want to do here!
11
12
time_left = 20 * 1;
13
progress.style.width = "100%";
14
timeleft.innerText = "20s Left";
15
}
16
}, 1000);
JavaScript
1
22
22
1
#progressbar {
2
text-align: center;
3
outline: none;
4
border: 2px solid black;
5
background-color: rgba(200, 200, 200, 0.5);
6
width 200px;
7
height: 30px;
8
}
9
10
#timeleft {
11
z-index: 100;
12
background-color: transparent;
13
font-family: 'Consolas';
14
}
15
16
#progress {
17
transition: 0.5s;
18
transform: translateY(-19px);
19
background-color: rgba(0, 200, 0, 0.7);
20
height: 100%;
21
width: 0%;
22
}
JavaScript
1
5
1
<div id="progressbar">
2
<b id="timeleft">20s Left</b>
3
<div id="progress">
4
</div>
5
</div>