1

i'm creating a countdown timer using jquery, i refered to a few example here and came out with the following codes. The problem is the time doesn't show, and i can't figure out what is wrong. Been stuck for almost 2 hours..

back code

    var day = 1;                        //countdown time
    var start = player.LastActive;      // Use UtcNow instead of Now
    var endTime = start.AddDays(day);   //endTime is a member, not a local variable
    Timespan remainingTime = endTime - DateTime.UtcNow;

my javascript

var endTime = '<%= remainingTime%>';
var startTime = '<%= DateTime.UtcNow %>'

$(document).load(function () {
        countDown(endTime);
    });

    function countDown(endTime) {
        if (endTime > 0) {
            var d = document.getElementById("countDiv");
            d.innerHTML = endTime;
            setTimeout(function () { countDown(endTime - 1); }, 1000);
        }
    }

my html

<div id="countDiv" style="float:left;margin-top: 13px; margin-left:5px;"></div>

my goal is to show how long more, for a player to get his daily move : which is a day after the players lastactive time. Can someone enlighten me..thank u

4 Answers 4

2

use $(window).load

or

$(document).ready

but not $(document).load

and as you seem to use jquery, you can do

$(document).ready(function () {
        countDown(endTime);
    });

    function countDown(endTime) {
        if (endTime > 0) {
            $('#countDiv').text(secondsToTime(endTime));
            setTimeout(function () { countDown(endTime - 1); }, 1000);
        }
    }
function secondsToTime(secs)
{
    var hours = Math.floor(secs / (60 * 60));

    var divisor_for_minutes = secs % (60 * 60);
    var minutes = Math.floor(divisor_for_minutes / 60);

    var divisor_for_seconds = divisor_for_minutes % 60;
    var seconds = Math.ceil(divisor_for_seconds);

    return hours +':' + minutes + ':' + seconds;
}

for seconds to time, it's coming from https://stackoverflow.com/a/6312999/961526 , you may take another version which you can find there.

see jsFiddle

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the reply but it's still the same after i followed yr method
@GopinathKoothaperumal well, maybe some problem with your endTime, because see : jsfiddle.net/rju6m/1
yes, i solved it. did this var endTime = '<%= remainingTime.totalseconds%>'; one last question, how do i convert the totalseconds into 00:00:00 this format
2

Since your endtime is string in your javascript code, convert it integer first

try this

$(document).ready(function () {
        countDown(parseInt(endTime));
    });

Comments

0

ready, not load.

$(document).ready(function () {
  countDown(endTime);
});

Comments

0

Or try this

  $(function() {
            countDown(endTime);
       });

 function countDown(parseInt(endTime)) {
        if (endTime > 0) {
            $('#countDiv').text(endTime);
            setTimeout(function () { countDown(endTime - 1); }, 1000);
        }
    }

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.