JavaScript Simple Error

Discussion in 'JavaScript' started by Pudge1, Aug 25, 2009.

  1. #1
    
    today = new Date();
    
    BigDay = new Date("October 31, 2009")
    msPerDay = 24 * 60 * 60 * 1000 ;
    timeLeft = (BigDay.getTime() - today.getTime());
    e_daysLeft = timeLeft / msPerDay;
    daysLeft = Math.floor(e_daysLeft);
    e_hrsLeft = (e_daysLeft - daysLeft)*24;
    hrsLeft = Math.floor(e_hrsLeft);
    minsLeft = Math.floor((e_hrsLeft - hrsLeft)*60);
    secsLeft = Math.floor((e_hrsLeft - hrsLeft)*60*60);
    document.write("" + daysLeft + " Days " + hrsLeft + " Hours " + minsLeft + " Minutes and " + secsLeft + " Seconds");
    
    Code (markup):
    I am new to JavaScript and I know the error I made in here is simple but I cannot pinpoint it. The problem is the seconds is like in the hundreds. Can someone please help me?
     
    Pudge1, Aug 25, 2009 IP
  2. andrew-bkk

    andrew-bkk Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can you be a bit more specific about the problem you are having.
     
    andrew-bkk, Aug 26, 2009 IP
  3. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #3
    Okay it is a countdown timer. It shows the days, hours, and minutes just fine. However it shows the seconds as like 800 but they still count down. So the problem is just the seconds.
     
    Pudge1, Aug 26, 2009 IP
  4. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #4
    Also can some one tell me how to make it auto update?
     
    Pudge1, Aug 27, 2009 IP
  5. jumboa7

    jumboa7 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think you forgot to subtract minutes left. Here is my solution.

    
    today = new Date();
    
    BigDay = new Date("October 31, 2009")
    msPerDay = 24 * 60 * 60 * 1000 ;
    timeLeft = (BigDay.getTime() - today.getTime());
    e_daysLeft = timeLeft / msPerDay;
    daysLeft = Math.floor(e_daysLeft);
    e_hrsLeft = (e_daysLeft - daysLeft)*24;
    hrsLeft = Math.floor(e_hrsLeft);
    e_minsLeft = (e_hrsLeft - hrsLeft) * 60;
    minsLeft = Math.floor(e_minsLeft);
    e_secsLeft = (e_minsLeft - minsLeft) * 60;
    secsLeft = Math.floor(e_secsLeft);
    document.write("" + daysLeft + " Days " + hrsLeft + " Hours " + minsLeft + " Minutes and " + secsLeft + " Seconds");
    
    Code (markup):
    See that I have added e_minsLeft and e_secsLeft. It follows the same logic to calculate days and months.
     
    jumboa7, Aug 27, 2009 IP
  6. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #6
    Thanks works perfectly. How to I make it so they auto refresh every second. Not like the whole page though.
     
    Pudge1, Aug 28, 2009 IP
  7. Mastermaniac

    Mastermaniac Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You can use the "setInterval" function!

    setInterval(function(),Interval)
     
    Mastermaniac, Aug 28, 2009 IP
  8. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #8
    Can I enclose all my code in a function?
    EDIT: For that I would have to make it update something but it has nothing to update.
     
    Pudge1, Aug 28, 2009 IP