I have a countdown which runs at a click event. I need to make this program object-oriented. I would like to create an object with a constructor so my countdown will be able to run in each div I want. If someone could show me the way to create a clear object with the constructor to call it in a main.js file where i will be able to put the id of the target div I want in parameters.
html
</button>
<div id="target">
</div>
<div id="targetbis">
</div>
js countdown
var timeout;
function countdownto(target, time, callback) {
var finish = new Date(time);
var s = 1000,
m = s * 60,
h = m * 60,
d = h * 24;
(function timer() {
var now = new Date();
var dist = finish - now;
var days = Math.floor(dist / d),
hours = Math.floor((dist % d) / h),
minutes = Math.floor((dist % h) / m),
seconds = Math.floor((dist % m) / s);
var timestring = minutes + ' minute(s) ' + seconds + ' seconde(s)';
target.innerHTML = timestring
clearTimeout(timeout);
if (dist > 0) {
timeout = setTimeout(timer, 1000);
} else {
callback()
}
})()
}
js which runs the countdown
let runningBtn = document.getElementById("runningbutton")
runningBtn.addEventListener("click", function(e) {
clearTimeout(timeout);
// countdown element
var countdownel = document.getElementById('target');
// 20 min
var time = new Date()
time.setSeconds(time.getSeconds() + 1200)
// countdown function call
countdownto(countdownel, time, function() {
alert('finished');
})
})
what I would like to have in a main.js
let newCountdown = new Countdown("target");
let newnewCountdown = new Countdown("targetbis");