473,225 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

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 2280
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

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

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
1
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote...
5
by: Allerdyce.John | last post by:
Hi, I have this piece of code which loops thru a STL list, but that causs an infinite loop. bool Executer::group(MyList& bl, ResultList & grl) { for (ExecuterList::iterator i =...
11
by: jojobar | last post by:
I have a aspx file (snippet shown below): ======= <td class="light-m1" id="rwCompleteButton" runat="server"><br/> <asp:ImageButton CssClass="clear-m1" runat="server" CommandName="Complete"...
10
by: Steven Woody | last post by:
i have a program which always run dead after one or two days, i think somewhere a piece of the code is suspicious of involving into a infinite loop. but for some reason, it is very hard to debug....
19
by: Richard | last post by:
Hi All, I copied a script example from http://www.irt.org/script/640.htm into a local .html file. I opened that file first in HTML-kit, which hung (in an infinite loop, I think) when I...
13
by: Sunbags | last post by:
Hello, I'm a 2nd year Computer Engineering student and I have a problem with my VB6 code. I've just started learning VB6 for a project in which we have to create a form which displays the...
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.