-1

I created a simple 1 Minute countdown, but it seems not to be working I don't know why.

IT GIVES EXPIRED as response instead of the timer showing.

// Set the date we're counting down to
var setoneminutetime = new Date();
setoneminutetime.setMinutes(1);
var countDownDate = new Date(setoneminutetime).getTime();
//set interval for the actual countdown
var x = setInterval(function() {
    var now = new Date().getTime();
    //end time minus the current time
    var distance = countDownDate - now;
    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    //show countdown  in div demo
    document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = "EXPIRED";
    }
}, 1000);
p {
    text-align: center;
    font-size: 60px;
    margin-top: 0px;
}
<p id="demo"></p>

5
  • Your setoneminutetime.setMinutes(1); set's Date object to be at :01, not 1 minute in future
    – Justinas
    Commented Jun 2, 2020 at 15:19
  • "The setMinutes() method sets the minutes for a specified date according to local time"
    – Andreas
    Commented Jun 2, 2020 at 15:19
  • @Justinas Ok, how do I correct that.
    – Shasha
    Commented Jun 2, 2020 at 15:20
  • @andreas Ok, learning
    – Shasha
    Commented Jun 2, 2020 at 15:21
  • "[still] learning" - that's ok, as a tip, javascript functions to generally do what they say on the tin (there are some annoying cases where this isn't the case and some that are a little confusing). In this case setMinutes sets the minutes... it's not addMinutes(1)
    – fdomn-m
    Commented Jun 2, 2020 at 15:26

3 Answers 3

3

setMinutes set the minutes to 1 of the current hour. Right now, it's 5:19PM, with setMinutes(1), the hour is 5:01PM. You need to get the current time and add 1 minute to it.

// Set the date we're counting down to
var setoneminutetime = new Date();
setoneminutetime.setTime(Date.now() + 1 * 60 * 1000); // Add 1 minutes to current timestamp
var countDownDate = new Date(setoneminutetime).getTime();
//set interval for the actual countdown
var x = setInterval(function() {
    var now = new Date().getTime();
    //end time minus the current time
    var distance = countDownDate - now;
    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    //show countdown  in div demo
    document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = "EXPIRED";
    }
}, 1000);
p {
    text-align: center;
    font-size: 60px;
    margin-top: 0px;
}
<p id="demo"></p>

2

As others have mentioned you've set the Date minute to :01 not added one minute. A simple solution would be to define countDownDate as now + 60*1000.

0
1

This is explicitly setting the "minute" value of the Date to 1:

setoneminutetime.setMinutes(1);

So this will only work if you load the page exactly at the 0 minute mark of any given hour. It looks like you intended to set it to the current minute plus 1:

setoneminutetime.setMinutes(setoneminutetime.getMinutes() + 1);

Note that this would also fail any time the current minute is 59. You should be able to get around this by creating a whole new date out of the total epoch miliseconds and adding 60,000 (miliseconds) to that:

var setoneminutetime = new Date(new Date().getTime() + 60000);

Not the answer you're looking for? Browse other questions tagged or ask your own question.