How to add a countdown script to a existing javascript clock?

Discussion in 'JavaScript' started by Xexi, Oct 19, 2008.

  1. #1
    So I have this code that someone else wrote and I have cron jobs setup to execute every 10 minutes.

    What I would lke to display is another timer counting down from the 10 minute mark below it.

    Example:
    10:14:22PM <~ Current script
    05:38 seconds until next event <~ I would like to add this

    //clock() recieves server time in php: 
    //javascript:clock(".date('G,i,s').") (24-hour format, Minutes, Seconds)
    
    var hours;
    var minutes;
    var seconds;
    var timer=null;
    var timed=null;
    function clock(hour,minute,second){
      hours=hour;
      minutes=minute;
      seconds=second;
      if(timer){clearInterval(timer);timer=null;}
      timer=setInterval("work();",1000);
    }
    
    function addZero(_v)
    {
      if(_v<10)_v="0"+_v;
      return _v;
    }
    
    function work(){
      if (!document.layers && !document.all && !document.getElementById) return;
      var runTime = new Date();
      var nd = "AM";
      var shours = hours;
      var sminutes = minutes;
      var sseconds = seconds;
      if (shours >= 12) {
        nd = "PM";
        shours-=12;
      }
      if (!shours) shours = 12;
      sminutes=addZero(sminutes);
      sseconds=addZero(sseconds);
      shours  =addZero(shours  );
      movingtime = ""+ shours + ":" + sminutes +":"+sseconds+"" + nd;
      if (document.getElementById) {
        document.getElementById("jsClock").innerHTML=movingtime;
      }
      else if (document.layers)
      {
        document.layers.clock.document.open();
        document.layers.clock.document.write(movingtime);
        document.layers.clock.document.close();
      }
      else if (document.all) {
        clock.innerHTML = movingtime;
      }
      if(++seconds>59) {
        seconds=0;
        if(++minutes>59)
        {
          minutes=0;
          if(++hours>23)
          {
            hours=0;
          }
        }
      }
    }
    Code (markup):
    Any help would be appreciated :)
     
    Xexi, Oct 19, 2008 IP
  2. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #2
    rhoula, Oct 19, 2008 IP
    Xexi likes this.
  3. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Take a look here
     
    xlcho, Oct 19, 2008 IP
    Xexi likes this.
  4. Xexi

    Xexi Well-Known Member

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    100
    #4
    wow, thank you both for your help! :)
     
    Xexi, Oct 20, 2008 IP