Reputation: 13
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.
Upvotes: 0
Views: 2155
Reputation: 2138
<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>
Upvotes: 0
Reputation: 318352
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);
});
Upvotes: 3