Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Darrel Lyons
Darrel Lyons
2,991 Points

Non-refresh timer

How can i make a countdown that will countdown without refreshing a div or a page. I have a function that makes thee time from the database but i want it to countdown on the page itself without refreshing any page. Here is the function :

function maketime($last){
$timenow = time();
            if($last>$timenow){
                    $order = $last-$timenow;
                        while($order >= 60){
                            $order = $order-60;
                            $ordermleft++;
                        }
                        while($ordermleft >= 60){
                            $ordermleft = $ordermleft-60;
                            $orderhleft++;
                        }

                        if($ordermleft == 0){
                            $ordermleft = "";
                        } else {
                        $ordermleft = "$ordermleft Minutes";
                        }
                        if($orderhleft == 0){
                            $orderhleft = "";
                        } else {
                        $orderhleft = "$orderhleft Hours";
                        }   
return "$orderhleft $ordermleft $order Seconds";
}}

3 Answers

Are you just wanting to learn how or needing one of these? You might want to check out FlipClock.js otherwise the code below is a basic example.

var newYear = new Date();
newYear = new Date(newYear.getFullYear() + 1, 1 - 1, 1);
$('#defaultCountdown').countdown({
    until: newYear
});

$('#removeCountdown').click(function () {
    var destroy = $(this).text() === 'Remove';
    $(this).text(destroy ? 'Re-attach' : 'Remove');
    $('#defaultCountdown').countdown(destroy ? 'destroy' : {
        until: newYear
    });
});

Source

Darrel Lyons
Darrel Lyons
2,991 Points

No , i have a timer set in the database for40 or sometimes 60seconds with time() + 40-60. I just need it to countdown without refreshing the page.