469,347 Members | 4,726 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,347 developers. It's quick & easy.

infinite loop problem

risk32
98
Hello. I tried to create an alarm clock program that would loop until the desired time, then it would open an audio file based upon user choice. The program itself works perfectly... that is, until it reaches the alarm time. Then it loops infinately, trying to open WMP every second. Anyone have a suggestion as to how I can change / fix this issue? The coding is at the bottom, and yes, I code a little differently with my if statements...

Expand|Select|Wrap|Line Numbers
  1. <html> 
  2. <head> 
  3. <script type="text/javascript">
  4. function time()
  5. {
  6. var date = new Date();  
  7. var hourNow = date.getHours();  
  8. var minuteNow = date.getMinutes(); 
  9. var secondsNow = date.getSeconds();   
  10. var weekday=new Array(7); 
  11. weekday[0]="Sunday"; 
  12. weekday[1]="Monday"; 
  13. weekday[2]="Tuesday"; 
  14. weekday[3]="Wednesday"; 
  15. weekday[4]="Thursday"; 
  16. weekday[5]="Friday"; 
  17. weekday[6]="Saturday";
  18.  
  19. if (secondsNow < 10)
  20. {
  21. secondsNow = "0" + secondsNow;   
  22.  
  23. document.getElementById("time").value = 'Current time is: ' + weekday[date.getDay()] + ', ' + hourNow + ':' + minuteNow + ':' + secondsNow;
  24.  setTimeout("time()",1000);
  25. }
  26.  
  27. function run() 
  28.  
  29. var alarmDay = document.getElementById("day").value; 
  30. var alarmHour = document.getElementById("hour").value; 
  31. var alarmMinute = document.getElementById("minute").value; 
  32.  
  33. var date = new Date();   
  34. var hourNow = date.getHours();   
  35. var minuteNow = date.getMinutes();  
  36. var secondsNow = date.getSeconds();
  37.  
  38.  
  39.  
  40. var weekday=new Array(7);  
  41. weekday[0]="Sunday";  
  42. weekday[1]="Monday";  
  43. weekday[2]="Tuesday";  
  44. weekday[3]="Wednesday";  
  45. weekday[4]="Thursday";  
  46. weekday[5]="Friday";  
  47. weekday[6]="Saturday"; 
  48.  
  49. var true1 = 0;
  50. var true2 = 0;
  51. var true3 = 0;
  52. var count = 0;
  53. var interval;
  54.  
  55.  
  56.  
  57.  
  58.  if (alarmDay == weekday[date.getDay()])
  59. {  
  60. true1 = 1;
  61. }
  62.  
  63. if (alarmDay != weekday[date.getDay()])
  64. {
  65. true1 = 0;
  66. }
  67.  
  68. if (alarmHour == hourNow)
  69. {
  70. true2 = 1;
  71. }
  72.  
  73. if (alarmHour != hourNow)
  74. {
  75. true2 = 0;
  76. }
  77.  
  78. if (alarmMinute == minuteNow)
  79. {
  80. true3 = 1;
  81. }
  82.  
  83. if (alarmMinute != minuteNow)
  84. {
  85. true3 = 0;
  86. }
  87.  
  88.  
  89.  
  90. if ((true1 == 0) || (true2 == 0) || (true3 == 0))
  91. {
  92. document.getElementById("alarm").value = "Alarm has been set";
  93. }
  94.  
  95.  
  96.  
  97. if ((true1 == 1) && (true2 == 1) && (true3 == 1))
  98. {
  99. count = count + 1;
  100. var path = document.getElementById("path").value;
  101. window.open(path);
  102. }
  103.  
  104. interval = setInterval("run()",1000);
  105.  
  106. if (count > 1)
  107. {
  108. clearInterval(interval);
  109. }
  110. }
  111. </script> 
  112. </head> 
  113. <body onload="time();"> 
  114.  
  115. <table bgcolor="gray" width=300> 
  116.  
  117. <td><font color="white"> 
  118. <center> 
  119. <textarea id="time" style="border:none; overflow:auto"></textarea> 
  120. <br> 
  121. Select the day you want the alarm to run.<br> 
  122. <select id="day"> 
  123. <option name="sunday" value="Sunday" >Sunday 
  124. <option name="monday" value="Monday" >Monday 
  125. <option name="tuesday" value="Tuesday" >Tuesday 
  126. <option name="wednesday" value="Wednesday" >Wednesday 
  127. <option name="thursday" value="Thursday" >Thursday 
  128. <option name="friday" value="Friday" >Friday 
  129. <option name="saturday" value="Saturday" >Saturday 
  130. </select> 
  131. </center> 
  132.  
  133. <center> 
  134. Select the time you want the alarm to run.<br> 
  135.  
  136. <select id="hour"> 
  137. <option name="00" value="00" >00 
  138. <option name="01" value="01" >01 
  139. <option name="02" value="02" >02 
  140. <option name="03" value="03" >03 
  141. <option name="04" value="04" >04 
  142. <option name="05" value="05" >05 
  143. <option name="06" value="06" >06 
  144. <option name="07" value="07" >07 
  145. <option name="08" value="08" >08 
  146. <option name="09" value="09" >09 
  147. <option name="10" value="10" >10 
  148. <option name="11" value="11" >11 
  149. <option name="12" value="12" >12 
  150. <option name="13" value="13" >13 
  151. <option name="14" value="14" >14 
  152. <option name="15" value="15" >15 
  153. <option name="16" value="16" >16 
  154. <option name="17" value="17" >17 
  155. <option name="18" value="18" >18 
  156. <option name="19" value="19" >19 
  157. <option name="20" value="20" >20 
  158. <option name="21" value="21" >21 
  159. <option name="22" value="22" >22 
  160. <option name="23" value="23" >23 
  161. </select> 
  162.  
  163. <select id="minute"> 
  164. <option name="00" value="0" >00 
  165. <option name="01" value="1" >01 
  166. <option name="02" value="2" >02 
  167. <option name="03" value="3" >03 
  168. <option name="04" value="4" >04 
  169. <option name="05" value="5" >05 
  170. <option name="06" value="6" >06 
  171. <option name="07" value="7" >07 
  172. <option name="08" value="8" >08 
  173. <option name="09" value="9" >09 
  174. <option name="10" value="10" >10 
  175. <option name="11" value="11" >11 
  176. <option name="12" value="12" >12 
  177. <option name="13" value="13" >13 
  178. <option name="14" value="14" >14 
  179. <option name="15" value="15" >15 
  180. <option name="16" value="16" >16 
  181. <option name="17" value="17" >17 
  182. <option name="18" value="18" >18 
  183. <option name="19" value="19" >19 
  184. <option name="20" value="20" >20 
  185. <option name="21" value="21" >21 
  186. <option name="22" value="22" >22 
  187. <option name="23" value="23" >23 
  188. <option name="24" value="24" >24 
  189. <option name="25" value="25" >25 
  190. <option name="26" value="26" >26 
  191. <option name="27" value="27" >27 
  192. <option name="28" value="28" >28 
  193. <option name="29" value="29" >29 
  194. <option name="30" value="30" >30 
  195. <option name="31" value="31" >31 
  196. <option name="32" value="32" >32 
  197. <option name="33" value="33" >33 
  198. <option name="34" value="34" >34 
  199. <option name="35" value="35" >35 
  200. <option name="36" value="36" >36 
  201. <option name="37" value="37" >37 
  202. <option name="38" value="38" >38 
  203. <option name="39" value="39" >39 
  204. <option name="40" value="40" >40 
  205. <option name="41" value="41" >41 
  206. <option name="42" value="42" >42 
  207. <option name="43" value="43" >43 
  208. <option name="44" value="44" >44 
  209. <option name="45" value="45" >45 
  210. <option name="46" value="46" >46 
  211. <option name="47" value="47" >47 
  212. <option name="48" value="48" >48 
  213. <option name="49" value="49" >49 
  214. <option name="50" value="50" >50 
  215. <option name="51" value="51" >51 
  216. <option name="52" value="52" >52 
  217. <option name="53" value="53" >53 
  218. <option name="54" value="54" >54 
  219. <option name="55" value="55" >55 
  220. <option name="56" value="56" >56 
  221. <option name="57" value="57" >57 
  222. <option name="58" value="58" >58 
  223. <option name="59" value="59" >59 
  224. </select> 
  225.  
  226.  
  227. <br> 
  228.  
  229. Enter the path for the sound file you wish to use<br> 
  230.  
  231. <input type="file" id="path" style="width:300px;"><br> 
  232.  
  233. <input type="reset" value="Reset" onclick="javascript: window.location.reload();"> 
  234. <input type="button" value="Exit" onclick="javascript: window.close();">
  235.  
  236. <input type="button" value="Run Alarm" onclick="run();"> 
  237.  
  238. </center> 
  239. </table>
  240. <textarea id="alarm" style="border:none; overflow:auto"></textarea> 
  241. </body> 
  242. </html> 
  243.  
Oct 8 '09 #1
5 2126
gits
5,390 Expert Mod 4TB
since you rerun the function in an interval and initialize the variable count within that function it will never be greater then 1 ... so the condition is always false

kind regards
Oct 9 '09 #2
risk32
98
@gits
Gits, where would be an effective place to put the count variable? I was thinking maybe before or after the
Expand|Select|Wrap|Line Numbers
  1. if ((true1 == 1) && (true2 == 1) && (true3 == 1)) 
portion of code, but I'm not sure it would make a difference.
Oct 9 '09 #3
gits
5,390 Expert Mod 4TB
the simplest way would be to make it global ... which means that the variable should be declared outside of your function ...

kind regards
Oct 12 '09 #4
risk32
98
@gits
So it should look something like this:

vairable...

function run()
{
...
}
Oct 13 '09 #5
gits
5,390 Expert Mod 4TB
yepp ... alike this :)
Oct 13 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

43 posts views Thread by Gremlin | last post: by
5 posts views Thread by mailpitches | last post: by
5 posts views Thread by Allerdyce.John | last post: by
10 posts views Thread by Steven Woody | last post: by
44 posts views Thread by James Watt | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.