Page 1 of 1

javascript timer edit

Posted: Tue Sep 24, 2013 12:28 pm
by nite4000
Hey everyone I have a javascript timer I am trying to edit. I need it to count down to a time and data. and what I am using it for is this. When a user does a action once done it will take the time and then count down 24hrs before they can do it again. not sure if the date would have anything to do with that or not but I am sure it would

I need to replace the date and time with the db variable.
I need to have it be sure its counting down from the next date. so if its Aug 10 at noon then it would count down from Aug 11 at noon

I hope someone can help

Here is my code. The code in bold is what I added not sure if I did it correctly.

Code: Select all

<script>
// set the date we're counting down to
var target_date = new Date("Aug 15, 2013").getTime();

[b]// I added this in
//set the time we're counting down to
var target_time = new Time("20:00:00").getTime();[/b]

// variables for time units
var days, hours, minutes, seconds;

// get tag element
var countdown = document.getElementById("countdown");

// update the tag with id "countdown" every 1 second
setInterval(function () {

    // find the amount of "seconds" between now and target
    var current_date = new Date().getTime();
    var seconds_left = (target_date - current_date) / 1000;

    // do some time calculations
    days = parseInt(seconds_left / 86400);
    seconds_left = seconds_left % 86400;
    
    hours = parseInt(seconds_left / 3600);
    seconds_left = seconds_left % 3600;
    
    minutes = parseInt(seconds_left / 60);
    seconds = parseInt(seconds_left % 60);
    
    // format countdown string + set tag value
    countdown.innerHTML = days + "d, " + hours + "h, " + minutes + "m, " + seconds + "s";   

}, 1000);
</script>

Re: javascript timer edit

Posted: Tue Sep 24, 2013 3:52 pm
by Christopher
Why are you using a timer to check if 24 hrs have passed? What if they close their browser (probable)? I would recommend checking a cookie with the date/time until they have access again. Or better, if they have an account, save the date/time in a database and have PHP do the check.