I've looked at a lot of questions on the site, but they don't seem to be for similar issues to mine.
I have a countdown timer written in Javascript that produces an array of weeks, days, hours etc. and calculates every second. The code I have in Countdown.js is:
var countdown = function(end, elements) {
var _second = 1000,
_minute = _second * 60,
_hour = _minute * 60,
_day = _hour * 24,
_week = _day * 7,
end = new Date(end),
timer,
calculate = function() {
var now = new Date(),
remaining = end.getTime() - now.getTime(),
data;
if(remaining <= 0) {
clearInterval(timer);
if(typeof callback === 'function') {
callback();
}
} else {
if(!timer) {
timer = setInterval(calculate, _second);
}
data = {
'weeks' : Math.floor(remaining / _week),
'days' : Math.floor((remaining % _week) / _day),
'hours' : Math.floor((remaining % _day) / _hour),
'minutes' : Math.floor((remaining % _hour) / _minute),
'seconds' : Math.floor((remaining % _minute) / _second)
}
if(elements.length) {
for(x in elements) {
var x = elements[x];
document.getElementById(x).innerHTML = data[x];
}
}
}
};
calculate();
}
I assume I have to make some changes to
if(elements.length) {
for(x in elements) {
var x = elements[x];
document.getElementById(x).innerHTML = data[x];
but not sure how to as the data[x]; gets in the way.
The HTML I have in index.html that relates to this is:
<script src="Countdown.js"></script>
<script>
countdown('06/03/2017 03:00:00 PM', ['weeks', 'days', 'hours', 'minutes', 'seconds'] , function() {
console.log('finished!')
});
</script>
and the weeks, days etc. are all in their individual DIVS like this:
<div style="width: 14%; padding-bottom: 14%; background-color: rgba(141,155,112,0.5); float: left;"><span id="weeks">00</span></div>
Any changes I make to the HTML do not stay as the Javascript takes over and the font reverts to normal. In addition, I would like to use a non standard font so is there a way of using @font-face with Javascript?