/    Sign up×
Community /Pin to ProfileBookmark

html+js countdown

I enclose the code I made. In the table there are three columns, the first reserved for the links to visit, the second reserved for the time that must pass before the next visit, the third reserved for the countdown. Then by clicking on the link in col.1 you should be redirected to the corresponding web page (in a new tab) while, at the same time, a countdown starts which takes the time indicated in col.2 and shows it in col.3 (expressed in hh : mm: ss); when it reaches 0, the word “ready” should appear, but it is also okay that it remains at 0.
Currently, when you click on the link, two new tabs open, one on the site indicated and the other at blank. The countdown starts, but I can’t get it to take the time indicated in col. 2 as a start. This all works for the first line only.
I would like some help fixing the script so that:
1) clicking on the link opens only one tab (the right one) and not two.
2) the countdown is able to take the initial value from the central column, as I said before.
3) this mechanism works for all links on the page (which will be many).
Thanks in advance.

[code]<!DOCTYPE html>
<html>
<head>
<style type=”text/css”>
body {
background-color:#7B68EE;
font-family: “Arial Rounded MT”,”Liberation Serif”, “School Times”,”Times New Roman”;
color:black;
font-weight:200;
font-size:110%
}
.centered table {
margin: 0 auto;
}
table {
width: 750px;
border: 4px solid #000000;
border-collapse: collapse
}
td {
width: 250px;
height: 40px;
border:1px solid black;
padding: .1em
}
td:nth-child(2) {
text-align:center
}
td:nth-child(3) {
text-align:center
}
a:link{
color:black;
text-decoration:none;
font-weight:bolder
}
a:visited {
color: black;
text-decoration:none;
}
a:hover {
color: red;
text-decoration:blink
}
a:active{
color: green;
text-decoration:underline
}
</style>
<script src=”/scripts/snippet-javascript-console.min.js?v=1″></script>
</head>
<body>
<table>
<tbody>
<tr>
<th>site
</th>
<th>timer (hh:mm:ss)
</th>
<th>countdown then ready
</th>
</tr>
<tr>
<td>
<a href=”http://www.google.com” id=”url” onclick=”startTimer()” target=”_blank”>
Google
</a>
</td>
<td>
01:30:<span id=”time”></span>
</td>
<td>
<span id=”countdown”></span>
</td>
</tr>
<tr>
<td>
<a href=”http://www.apple.com” id=”url” onclick=”startTimer()” target=”_blank”>
Apple
</a>
</td>
<td>
01:30:<span id=”time”></span>
</td>
<td>
<span id=”countdown”></span>
</td>
</tr>
<tr>
<td>
<a href=”http://www.microsoft.com” id=”url” onclick=”startTimer()” target=”_blank”>
Microsoft
</a>
</td>
<td>
12:01:30:<span id=”time”></span>
</td>
<td>
<span id=”countdown”></span>
</td>
</tr>
</tbody>
</table>
<script type=”text/javascript”>
$ = (id) => { return document.getElementById(id) }

let secs = 30,
link = $(‘url’),
time = $(‘time’),
count = $(‘countdown’);

time.innerHTML = secs;
count.innerHTML = secs;

startTimer = () => {

window.open(url.href, ‘_blank’);

let tiks = setInterval(countdown, 1000);

link.style.color = ‘red’;
count.style.color = ‘red’;

function countdown() {
if (secs == -1) {
clearInterval(tiks)
link.style.color = ‘green’;
time.style.color = ‘green’;
count.style.color = ‘green’;
} else {
count.innerHTML = secs;
secs–;
}
}

}
</script>
</body>
</html>[/code]

to post a comment
HTMLJavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@sibertOct 29.2020 — I do not understand what you are trying to do, but jsfiddle complains about using id more than once:

https://jsfiddle.net/etxr39u5/
Copy linkTweet thisAlerts:
@bigmimauthorOct 29.2020 — I have clearly written (I hope) in the message what I am trying to do, sorry if in js I am so poor.
Copy linkTweet thisAlerts:
@SempervivumOct 29.2020 — @bigmim#1624691 Single backtics don't work reliably when posting code. You better use code tags: `your code here` or triple backtics.

I edited your posting accordingly.
Copy linkTweet thisAlerts:
@bigmimauthorOct 29.2020 — @Sempervivum :Thank you.
Copy linkTweet thisAlerts:
@sibertOct 30.2020 — > @bigmim#1624694 I have clearly written (I hope) in the message what I am trying to do, sorry if in js I am so poor.

id="time" etc and other id's are not unique which could affect the functionality. Use class="time" OR id="time1", id="time2" etc instead.
Copy linkTweet thisAlerts:
@SempervivumOct 30.2020 — @bigmim#1624691 Follow siberts recommendation and use classes instead of IDs:
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th&gt;site
&lt;/th&gt;
&lt;th&gt;timer (hh:mm:ss)
&lt;/th&gt;
&lt;th&gt;countdown then ready
&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.google.com" class="link" target="_blank"&gt;
Google
&lt;/a&gt;
&lt;/td&gt;
&lt;td class="time"&gt;
01:30
&lt;/td&gt;
&lt;td class="countdown"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.apple.com" class="link" target="_blank"&gt;
Apple
&lt;/a&gt;
&lt;/td&gt;
&lt;td class="time"&gt;
01:30
&lt;/td&gt;
&lt;td class="countdown"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.microsoft.com" class="link" target="_blank"&gt;
Microsoft
&lt;/a&gt;
&lt;/td&gt;
&lt;td class="time"&gt;
01:30
&lt;/td&gt;
&lt;td class="countdown"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

Then add an eventlistener to each of the links and you can get the parameters by this javascript:
&lt;script&gt;
document.querySelectorAll('table td a').forEach(link =&gt; {
link.addEventListener('click', event =&gt; {
event.preventDefault();
const row = link.parentNode.parentNode;
const url = link.href;
const time = row.querySelector('.time');
const countdown = row.querySelector('.countdown');
console.log(url, time.classList, countdown.classList);
// now all elements are ready
// start timer
})
});
&lt;/script&gt;
Copy linkTweet thisAlerts:
@bigmimauthorOct 30.2020 — @Sempervivum - Thank you very much!
Copy linkTweet thisAlerts:
@bigmimauthorOct 31.2020 — I have tried in every way to fix my script but in vain, my javascript is really poor. This is the code I have arranged, but it doesn't work! Further help would be appreciated, thanks.

<code>

<table>

<tbody>

<tr>

<th>site

</th>

<th>time

</th>

<th>countdown

</th>

</tr>

<tr>

<td>

<a href="http://www.google.com" id="url" onclick="startTimer()" target="_blank">

Google

</a>

</td>

<td class="time">01:30:00

</td>

<td class="countdown">

</td>

</tr>

<tr>

<td>

<a href="http://www.apple.com" id="url" onclick="startTimer()" target="_
blank">

Apple

</a>

</td>

<td class="time">00:30:15

</td>

<td class="countdown">

</td>

</tr>

<tr>

<td>

<a href="http://www.microsoft.com" id="url" onclick="startTimer()" target="_blank">

Microsoft

</a>

</td>

<td class="time">00:00:20

</td>

<td class="countdown">

</td>

</tr>

</tbody>

</table>

</code>

<code>

<script>

document.querySelectorAll('table td a').forEach(link => {

link.addEventListener('click', event => {

event.preventDefault();

const row = link.parentNode.parentNode;

const url = link.href;

const time = row.querySelector('.time');

const countdown = row.querySelector('.countdown');

console.log(url, time.classList, countdown.classList);

// now all elements are ready

// start timer

$ = (id) => { return document.getElementById(id) }

link = $('url'),

time = $('time'),

count = $('countdown');

time.innerHTML = secs;

count.innerHTML = secs;

startTimer = () => {

window.open(url.href, '_
blank');

let tiks = setInterval(countdown, 1000);

link.style.color = 'red';

count.style.color = 'red';

function countdown() {

if (secs == -1) {

clearInterval(tiks)

link.style.color = 'green';

time.style.color = 'green';

count.style.color = 'green';

} else {

count.innerHTML = secs; secs--;

}

}

}

})

});

</script>

</code>
Copy linkTweet thisAlerts:
@bigmimauthorOct 31.2020 — I tried in every way to fix my script but in vain, my javascript is really poor. This is the code I have arranged, but it doesn't work! Further help would be appreciated, thanks.

``<i>
</i>&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th&gt;site
&lt;/th&gt;
&lt;th&gt;time
&lt;/th&gt;
&lt;th&gt;countdown
&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.google.com" id="url" onclick="startTimer()" target="_blank"&gt;
Google
&lt;/a&gt;
&lt;/td&gt;
&lt;td class="time"&gt;01:30:00
&lt;/td&gt;
&lt;td class="countdown"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.apple.com" id="url" onclick="startTimer()" target="_blank"&gt;
Apple
&lt;/a&gt;
&lt;/td&gt;
&lt;td class="time"&gt;00:30:15
&lt;/td&gt;
&lt;td class="countdown"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.microsoft.com" id="url" onclick="startTimer()" target="_blank"&gt;
Microsoft
&lt;/a&gt;
&lt;/td&gt;
&lt;td class="time"&gt;00:00:20
&lt;/td&gt;
&lt;td class="countdown"&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;



&lt;script&gt;
document.querySelectorAll('table td a').forEach(link =&gt; {
link.addEventListener('click', event =&gt; {
event.preventDefault();
const row = link.parentNode.parentNode;
const url = link.href;
const time = row.querySelector('.time');
const countdown = row.querySelector('.countdown');
console.log(url, time.classList, countdown.classList);
// now all elements are ready
// start timer
$ = (id) =&gt; { return document.getElementById(id) }
link = $('url'),
time = $('time'),
count = $('countdown');
time.innerHTML = secs;
count.innerHTML = secs;
startTimer = () =&gt; {
window.open(url.href, '_blank');
let tiks = setInterval(countdown, 1000);
link.style.color = 'red';
count.style.color = 'red';
function countdown() {
if (secs == -1) {
clearInterval(tiks)
link.style.color = 'green';
time.style.color = 'green';
count.style.color = 'green';
} else {
count.innerHTML = secs; secs--;
}
}
}
})
});
&lt;/script&gt;<i>
</i>
``
Copy linkTweet thisAlerts:
@SempervivumOct 31.2020 — After having changed the IDs to classes the javascript needs update too. Try this:
&lt;script&gt;
let timerActive = false, startTime = null, timer = null;
// get all links in the table and iterate over
document.querySelectorAll('table td a').forEach(link =&gt; {
// add event listener for click
link.addEventListener('click', event =&gt; {
// prevent default action (calling link in the same page)
event.preventDefault();
// check if a timer is active already
if (!timerActive) {
// no timer active currently
timerActive = true;
// get current row, i. e. grandparent of link
const row = link.parentNode.parentNode;
const url = link.href;
// get table cell containing time
const time = row.querySelector('.time');
// parse the time
const timeArr = time.textContent.split(':');
let timeVal = 0;
for (let i = 0; i &lt; timeArr.length; i++) {
timeVal *= 60;
timeVal += parseInt(timeArr[i]);
}
console.log(timeVal);
// note start time
startTime = Date.now();
// get table cell containing countdown
const countdown = row.querySelector('.countdown');
// open link in new tab
window.open(url, '_blank');
// start cyclical timer
timer = setInterval(() =&gt; {
// calculate remaining seconds
const remaining = Math.floor(timeVal - (Date.now() - startTime) / 1000);
// has the remaining time not yet reached 0?
if (remaining &gt; 0) {
// update countdown
countdown.textContent = remaining;
} else {
countdown.textContent = 'ready';
// cancel cyclical timer
timerActive = false;
clearInterval(timer);
}
}, 100);
}
});
});
&lt;/script&gt;
Copy linkTweet thisAlerts:
@bigmimauthorOct 31.2020 — @Sempervivum#1624747 Great! Now it works, only a last thing: the other links, and related countdowns, should start even while those already clicked are still counting and the countdown should be expressed in hh: mm: ss. Sorry if I'm asking too much. Thank you for your kindness.
Copy linkTweet thisAlerts:
@SempervivumOct 31.2020 — You're welcome!

Check if this scripts does the job:
function format(val) {
return String(val).padStart(2, '0');
}
let timerActive = [], startTime = [], timer = [];
// get all links in the table and iterate over
document.querySelectorAll('table td a').forEach((link, idx) =&gt; {
// add event listener for click
link.addEventListener('click', event =&gt; {
// prevent default action (calling link in the same page)
event.preventDefault();
// check if a timer is active already
if (!timerActive[idx]) {
// no timer active currently
timerActive[idx] = true;
// get current row, i. e. grandparent of link
const row = link.parentNode.parentNode;
const url = link.href;
// get table cell containing time
const time = row.querySelector('.time');
// parse the time
const timeArr = time.textContent.split(':');
let timeVal = 0;
for (let i = 0; i &lt; timeArr.length; i++) {
timeVal *= 60;
timeVal += parseInt(timeArr[i]);
}
console.log(timeVal);
// note start time
startTime[idx] = Date.now();
// get table cell containing countdown
const countdown = row.querySelector('.countdown');
// open link in new tab
window.open(url, '_blank');
// start cyclical timer
timer = setInterval(() =&gt; {
// calculate remaining seconds
const remaining = Math.floor(timeVal * 1000 - (Date.now() - startTime[idx]));
// has the remaining time not yet reached 0?
if (remaining &gt; 0) {
// update countdown
let rem = remaining / 1000;
const sec = Math.floor(rem % 60);
rem /= 60;
const min = Math.floor(rem % 60);
rem /= 60;
const hour = Math.floor(rem);
countdown.textContent = format(hour) + ':' + format(min) + ':' + format(sec);
} else {
countdown.textContent = 'ready';
// cancel cyclical timer
timerActive[idx] = false;
clearInterval(timer[idx]);
}
}, 100);
}
});
});
Copy linkTweet thisAlerts:
@bigmimauthorOct 31.2020 — @Sempervivum#1624751 That's pretty nice. I wish I had your skills. I will have to work hard. Thank you again.
×

Success!

Help @bigmim spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...