0

i need help with my countdown timer. i have a unix timestamp that i want to countdown from in minutes. it should just print out "X minutes left." in my span. i dont know why it isn't working. i placed the jquery above it, so jquery is in use.

HTML:

<span id="time-left">x</span>

JS:

<script type="text/javascript">
$(document).ready(function() {
    setTimeout(function(){
        var endTime = 1450992799399;
        var curTime = Math.floor((new Date()).getTime() / 1000);
        var seconds = endTime - curTime;
        var minutes = Math.floor(seconds / 60);
        seconds %= 60;
        $('#time-left').html() = minutes + " minutes left.";
    }, 1000);
});

And I want it to update every second. But right now it isn't doing anything. The text remains as "x" in my span.

0

2 Answers 2

3

jQuery uses methods, not properties, so it's

$(element).html('HTML to set');

not

$(element).html() = 'HTML to set'; 

Giving you

$(document).ready(function() {
    setTimeout(function(){
        var endTime = 1450992799399;
        var curTime = Date.now();
        var seconds = curTime - endTime;
        var minutes = Math.floor((seconds / 60) / 1000);

        $('#time-left').html( minutes + " minutes left.");
    }, 1000);
});
Sign up to request clarification or add additional context in comments.

6 Comments

Oh I see, thanks! Now it kinda works. But it shows "24159028708 minutes left.". I guess my conversion from UNIX timestamp messed up completely. lol
You don't have a unix timestamp in seconds, 1450992799399 is milliseconds, the same thing javascript returns, so stop dividing by a thousand
I'm kinda confused, I don't even know what a UNIX timestamp really is. My friend told me this (1450992799399) was a "unix timestamp" and that it is referring to a date (today, in about 15 minutes or so). I dont need to split by 1000? It shows me some completely different number now. Im confused
So just var curTime = new Date().getTime(); ?
And that was probably correct when your friend told you that, but those 15 minutes have past now. A unix timestamp is a moment in time, it's the number of seconds since 1. january 1970.
|
0
<script>
    var countdown = new Date("May 28, 2018 12:00:00").getTime();
    var x = setInterval(function(){
        var now = new Date().getTime();
        var distance = countdown - 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);
        document.getElementById("timer").innerHTML = "D " + days + " H " +  hours + " M " + minutes + " S " + seconds;
    },1000)
    if(distance < 0){
        clearInterval(x);
        document.getElementById("timer").innerHTML = "EXPIRED";
    }
</script>
<div id="timer" class="timer"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.