-3

I am having a problem with a countdown. I have made the countdown however the JS to change the HTML refuses to loop. I have a setInterval and I have tried for, while and do loops but none work. I would really appreciate some help.

HTML:

<body>
    <center>
    <h1> Input the date and time you want to countdown to!</h1>
    <form>
        Second:<input id="seconds" type="number"><br>
        Minute:<input id="minutes" type="number"><br>
        Hour:<input id="hours" type="number"><br>
        Day:<input id="days" type="number"><br>
        Month:<input id="months" type="text"><br>
        Year:<input id="years" type="number"><br>
    </form>
    <button onclick="start()">Calculate!</button>
    <h1 id="yearsres"></h1>Years<br>
    <h1 id="monthsres"></h1>Months<br>
    <h1 id="daysres"></h1>Days<br>
    <h1 id="hoursres"></h1>Hours<br>
    <h1 id="minutesres"></h1>Minutes<br>
    <h1 id="secondsres"></h1>Seconds<br>
    </center>
</body>

JS:

function start() {
    var myVar = setInterval(test(), 1000)
}

function test() {
    console.log("hi");
}

function calculateseconds(sec) {
    var year = document.getElementById("years").value;
    var month = document.getElementById("months").value;
    var day = document.getElementById("days").value;
    var hour = document.getElementById("hours").value;
    var minute = document.getElementById("minutes").value;
    var second = document.getElementById("seconds").value;
    var countdownto = new Date(month + " " + day + "," + " " + year + " " + hour + ":" + minute + ":" + second);
    var epochto = countdownto.getTime()/1000.0;
    var current = new Date();
    var epochcurrent = current.getTime()/1000.0;
    var epochcountdown = epochto - epochcurrent;
    var t = parseInt(epochcountdown);
    var years = 0;
    var months = 0;
    var days = 0;
    var i = 1;

    if(t>31556926){
        years = parseInt(t/31556926); t = t-(years*31556926);       
    }
    if(t>2629743){
        months = parseInt(t/2629743); t = t-(months*2629743);   
    }
    if(t>86400){
        days = parseInt(t/86400); t = t-(days*86400);
    }
    var hours = parseInt(t/3600);
    t = t-(hours*3600);
    var minutes = parseInt(t/60);
    t = t-(minutes*60);


    document.getElementById("yearsres").innerHTML = years;
    document.getElementById("monthsres").innerHTML = months;
    document.getElementById("daysres").innerHTML = days;
    document.getElementById("hoursres").innerHTML = hours;
    document.getElementById("minutesres").innerHTML = minutes;
    document.getElementById("secondsres").innerHTML = t;

}
Skaranjit
  • 588
  • 4
  • 24

2 Answers2

1

By adding () to your test function you call it. setInterval method takes a reference to the function (name of the function) and interval in milliseconds. It can be used like this:

function start() {
    var myVar = setInterval(test, 1000);
}

Or like this:

function start() {
    var myVar = setInterval(function() {
        test();
    }, 1000);
}

Also if you need to pass parameters to your function you can do it like this:

function start() {
    var myVar = setInterval(test, 1000, "First param", "Second param");
}
ilian6806
  • 171
  • 5
0

Remove () from test() somehow it will work

https://jsfiddle.net/alesmana/u30zmj3t/

Aditya
  • 757
  • 8
  • 11