new Date() problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • risk32
    New Member
    • Mar 2008
    • 98

    #16
    I did a secondary test to see why the alerts were always failing. I used the document.write for currentDay and day to see what values they were holding. currentDay would return the correct day while day would return the first value in the select. Should I include an onChange function to change the value?

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #17
      The reason is quite simple. This code:
      Code:
      <select>
      <option name="sunday" value="sunday" id="day">Sunday
      <option name="monday" value="monday" id="day">Monday
      <option name="tuesday" value="tuesday" id="day">Tuesday
      <option name="wednesday" value="wednesday" id="day">Wednesday
      <option name="thursday" value="thursday" id="day">Thursday
      <option name="friday" value="friday" id="day">Friday
      <option name="saturday" value="saturday"id="day" >Saturday
      </select>
      is incorrect. You cannot have multiple IDs of the same name. They have to be unique. Give this ID to the select element instead:
      Code:
      <select id="day">

      Comment

      • risk32
        New Member
        • Mar 2008
        • 98

        #18
        Thanks acoder. I make some stupid mistakes sometimes. Is there a way to compare values and have a countdown timer for them to activate an event when the timer reaches 0? What I mean is, I can get the program to work if I run the program at the exact day, hour and minute. But if I don't, I'll need a function to get the milliseconds of the time it is now, figure out what that value would be for the alarm, subtract the values for the difference, and create a timer to countdown. Easier said than done in my opinion, but then I'm new to this.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #19
          You can use the setTimeout/setInterval methods. You can use setTimeout if you just want it to run without comparison based on the current time and the alarm time.

          Comment

          • risk32
            New Member
            • Mar 2008
            • 98

            #20
            I'm having a problem with my .js file. I've got the countdown procedure for the alarm, but a setTimeout won't work. I used setTimeout("run ()", 1000); at the end of the function just before the last bracket. I figured out how to do the simple arithmetic with the remaining time, except for if the current time is in the next week. Still trying to work that out, but I haven't been able to come up with a solution. Any help on this? Here's the code that I have so far:
            Code:
            function time() 
            { 
              
             
            var date = new Date(); 
            var hourNow = date.getHours(); 
            var minuteNow = date.getMinutes();
            var secondsNow = date.getSeconds(); 
            
            
            var weekday=new Array(7);
            weekday[0]="Sunday";
            weekday[1]="Monday";
            weekday[2]="Tuesday";
            weekday[3]="Wednesday";
            weekday[4]="Thursday";
            weekday[5]="Friday";
            weekday[6]="Saturday";
              
            if (secondsNow < 10)
            {
            secondsNow = "0" + secondsNow;
            } 
            
            
            if (minuteNow < 10)
            {
            minuteNow = "0" + minuteNow;
            }
            
            document.getElementById("time").value = 'Current time is: ' + weekday[date.getDay()] + ', ' + hourNow + ':' + minuteNow + ':' + secondsNow; 
            
              
              
            setTimeout("time()",1000); 
              
            }
            
            
            
            
            function run()
            {
            
            var date = new Date(); 
            
            var dayCurrent;
            var dayLeft;
            var dayAlarm;
            
            var hourLeft;
            var hourAlarm;
            var hourCurrent;
            
            var minuteLeft;
            var minuteAlarm;
            var minuteCurrent;
            
            var timeLeft;
            
            dayCurrent = date.getDay();
            dayAlarm = document.getElementById("day").selectedIndex;
            
            hourCurrent = date.getHours();
            hourAlarm = document.getElementById("hour").selectedIndex;
            
            minuteCurrent = date.getMinutes();
            minuteAlarm = document.getElementById("minute").selectedIndex;
            
            if ((dayAlarm > dayCurrent) || (dayAlarm = dayCurrent))
            {
            dayLeft = dayAlarm - dayCurrent;
            }
            
            else
            {
            dayLeft = 7 - (dayCurrent - dayAlarm);
            }
            
            
            if ((hourAlarm > hourCurrent) || (hourAlarm = hourCurrent))
            {
            hourLeft = hourAlarm - hourCurrent;
            }
            
            else
            {
            hourLeft = 24 - (hourCurrent - hourAlarm);
            }
            
            if ((minuteAlarm > minuteCurrent) || (minuteAlarm = minuteCurrent))
            {
            minuteLeft = minuteAlarm - minuteCurrent - 1;
            }
            
            else
            {
            minuteLeft = 60 - (minuteCurrent - minuteAlarm);
            }
            
            var secondsCurrent = date.getSeconds();
            var secondsLeft = 60 - secondsCurrent;
            
            
            
            
            
            timeLeft = dayLeft + " days " + hourLeft + " hours " + minuteLeft + " minutes " + secondsLeft + " seconds";
            
            document.getElementById("timeLeft").value = timeLeft;
            
            if ((dayLeft = 0) && (hourLeft = 0) && (minuteLeft = 0) && (secondsLeft = 0))
            {
            var path = document.getElementById("path").value;
            path = '<a href="' + path + '/>';
            window.open(path);
            }
            
            
            }

            Comment

            • risk32
              New Member
              • Mar 2008
              • 98

              #21
              Still can't figure it out...

              So, this is what I finally came up with. The upper JS code works perfectly, but when I went to run it, it pointed to line 181, Char 1 with a 'Object Expected' code in IE... which led me to believe that either the html for the button to run the alarm is wrong (which I highly doubt) or it's something in the 'function run()' portion. I have no idea what it is... Could anybody help me to figure out what is wrong?



              Code:
              <html> 
              <head> 
              <script type="text/javascript">
              function time()
              {
              var date = new Date();  
              var hourNow = date.getHours();  
              var minuteNow = date.getMinutes(); 
              var secondsNow = date.getSeconds();   
              var weekday=new Array(7); 
              weekday[0]="Sunday"; 
              weekday[1]="Monday"; 
              weekday[2]="Tuesday"; 
              weekday[3]="Wednesday"; 
              weekday[4]="Thursday"; 
              weekday[5]="Friday"; 
              weekday[6]="Saturday"; 
              
              document.getElementById("time").value = 'Current time is: ' + weekday[date.getDay()] + ', ' + hourNow + ':' + minuteNow + ':' + secondsNow;
              setTimeout("time()",1000);
              }
              </script> 
              </head> 
              <body onload="time();"> 
                
              <table bgcolor="gray" width=300> 
                
              <td><font color="white"> 
              <center> 
              <textarea id="time" style="border:none; overflow:auto"></textarea> 
              <br> 
              Select the day you want the alarm to run.<br> 
              <select id="day"> 
              <option name="sunday" value="sunday" >Sunday 
              <option name="monday" value="monday" >Monday 
              <option name="tuesday" value="tuesday" >Tuesday 
              <option name="wednesday" value="wednesday" >Wednesday 
              <option name="thursday" value="thursday" >Thursday 
              <option name="friday" value="friday" >Friday 
              <option name="saturday" value="saturday" >Saturday 
              </select> 
              </center> 
                
              <center> 
              Select the time you want the alarm to run.<br> 
                
              <select id="hour"> 
              <option name="00" value="00" >00 
              <option name="01" value="01" >01 
              <option name="02" value="02" >02 
              <option name="03" value="03" >03 
              <option name="04" value="04" >04 
              <option name="05" value="05" >05 
              <option name="06" value="06" >06 
              <option name="07" value="07" >07 
              <option name="08" value="08" >08 
              <option name="09" value="09" >09 
              <option name="10" value="10" >10 
              <option name="11" value="11" >11 
              <option name="12" value="12" >12 
              <option name="13" value="13" >13 
              <option name="14" value="14" >14 
              <option name="15" value="15" >15 
              <option name="16" value="16" >16 
              <option name="17" value="17" >17 
              <option name="18" value="18" >18 
              <option name="19" value="19" >19 
              <option name="20" value="20" >20 
              <option name="21" value="21" >21 
              <option name="22" value="22" >22 
              <option name="23" value="23" >23 
              </select> 
                
              <select id="minute"> 
              <option name="00" value="00" >00 
              <option name="01" value="01" >01 
              <option name="02" value="02" >02 
              <option name="03" value="03" >03 
              <option name="04" value="04" >04 
              <option name="05" value="05" >05 
              <option name="06" value="06" >06 
              <option name="07" value="07" >07 
              <option name="08" value="08" >08 
              <option name="09" value="09" >09 
              <option name="10" value="10" >10 
              <option name="11" value="11" >11 
              <option name="12" value="12" >12 
              <option name="13" value="13" >13 
              <option name="14" value="14" >14 
              <option name="15" value="15" >15 
              <option name="16" value="16" >16 
              <option name="17" value="17" >17 
              <option name="18" value="18" >18 
              <option name="19" value="19" >19 
              <option name="20" value="20" >20 
              <option name="21" value="21" >21 
              <option name="22" value="22" >22 
              <option name="23" value="23" >23 
              <option name="24" value="24" >24 
              <option name="25" value="25" >25 
              <option name="26" value="26" >26 
              <option name="27" value="27" >27 
              <option name="28" value="28" >28 
              <option name="29" value="29" >29 
              <option name="30" value="30" >30 
              <option name="31" value="31" >31 
              <option name="32" value="32" >32 
              <option name="33" value="33" >33 
              <option name="34" value="34" >34 
              <option name="35" value="35" >35 
              <option name="36" value="36" >36 
              <option name="37" value="37" >37 
              <option name="38" value="38" >38 
              <option name="39" value="39" >39 
              <option name="40" value="40" >40 
              <option name="41" value="41" >41 
              <option name="42" value="42" >42 
              <option name="43" value="43" >43 
              <option name="44" value="44" >44 
              <option name="45" value="45" >45 
              <option name="46" value="46" >46 
              <option name="47" value="47" >47 
              <option name="48" value="48" >48 
              <option name="49" value="49" >49 
              <option name="50" value="50" >50 
              <option name="51" value="51" >51 
              <option name="52" value="52" >52 
              <option name="53" value="53" >53 
              <option name="54" value="54" >54 
              <option name="55" value="55" >55 
              <option name="56" value="56" >56 
              <option name="57" value="57" >57 
              <option name="58" value="58" >58 
              <option name="59" value="59" >59 
              </select> 
                
              
              <br> 
                
              Enter the path for the sound file you wish to use<br> 
              
              <input type="file" id="path" style="width:300px;"><br> 
                
              <input type="reset" value="Reset" onclick="javascript: window.location.reload();"> 
              <input type="button" value="Exit" onclick="javascript: window.close();">
              <script type="javascript/text">
              function run() 
              { 
                
              var alarmDay = document.getElementById("day").value;
              var alarmHour = document.getElementById("hour").value;
              var alarmMinute = document.getelementById("minute").value;
              
              var date = new Date();  
              var hourNow = date.getHours();  
              var minuteNow = date.getMinutes(); 
              var secondsNow = date.getSeconds();  
                
                
              var weekday=new Array(7); 
              weekday[0]="Sunday"; 
              weekday[1]="Monday"; 
              weekday[2]="Tuesday"; 
              weekday[3]="Wednesday"; 
              weekday[4]="Thursday"; 
              weekday[5]="Friday"; 
              weekday[6]="Saturday";
              
              if ((weekday[date.getDay()] == alarmDay) && (hourNow == alarmDay) && (minuteNow == alarmMinute))
              {
              var path = document.getElementById("path").value; 
              path = '<a href="' + path + '/>'; 
              window.open(path); 
              }
              
              setTimeout("run()",1000);
                
                
              }
              </script>
              <input type="button" value="Run Alarm" onclick="run();"> 
              
              </center> 
              </table> 
              </body> 
              </html>

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #22
                Line 152:
                Code:
                var alarmMinute = document.get[b][u]E[/u][/b]lementById("minute").value;

                Comment

                • risk32
                  New Member
                  • Mar 2008
                  • 98

                  #23
                  That's my mistake, I caught it after I posted. I have a new problem with an infinate loop though.

                  Comment

                  Working...