Reputation: 1519
I am working on a clock in Javascript in which seconds is getting displayed after every 5 seconds which I am not sure why.
The code which I have used for the clock is:
const suv = document.querySelector('#online-suv');
const suvLabel = document.querySelector('#online-suvLabel');
suv.title = suvLabel.title = headingFromDurationstamp(newT, true);
function headingFromDurationstamp(t, inSeconds) {
let m = new Date(Number(t));
let sac = ('0' + String(d.getHours())).substr(-2) + ':' + ('0' + String(d.getMinutes())).substr(-2);
if (inSeconds) {
sac += ':' + ('0' + String(d.getSeconds())).substr(-2);
}
m = undef;
return sac;
}
Problem Statement:
I am wondering what changes I need to make in the JS above so that the seconds get displayed every second like a normal clock like this.
Upvotes: 0
Views: 230
Reputation: 1431
Your logic works perfectly fine. You just have to change few variable misuse like you assigned m
to new date and use d
out of nowhere to get sac and some d = undef
which i'm not sure what for.
Also please check your loop.
setInterval(function() {
$('.time').html(headingFromDurationstamp(Date.now(), true));
}, 500);
function headingFromDurationstamp(t, inSeconds) {
let d = new Date(Number(t));
let sac = ('0' + String(d.getHours())).substr(-2) + ':' + ('0' + String(d.getMinutes())).substr(-2);
if (inSeconds) {
sac += ':' + ('0' + String(d.getSeconds())).substr(-2);
}
//d = undef;
return sac;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="time">00:00:00</div>
Upvotes: 1