944,138 Members | Top Members by Rank

Ad:
Dec 4th, 2006
0

Javascript - how to stop/halt/break this loop/script plss

Expand Post »
Hi,

I'm a newbie on javascript, and am experimenting with this simple script below.
I was wondering how could I halt/quite/close the script/program after pressing the X or CANCEL button?

I try to press X and CANCEL button, but it won't quit and this script results in infinite loop

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html><head><title>Phone Cost</title></head>
  2. <body on>
  3.  
  4. <script>
  5. var i
  6. var minutes
  7. var monthly
  8. var excess
  9. var charge
  10. var total
  11.  
  12. while (minutes=1)
  13.  
  14. {
  15. minutes = prompt("Minutes Used:");
  16. alert("Your entery was: "+minutes+"");
  17.  
  18. monthly=40
  19. excess=minutes-600
  20. charge=excess*0.49
  21. total=monthly+charge
  22.  
  23. if(minutes>=600)
  24. {
  25. document.write("Your Minutes Uses is: "+minutes+" <br>");
  26. document.write("Excess Minutes Uses is: "+excess+" <br>");
  27. document.write("Monthly Charges is: $"+monthly+".00<br>");
  28. document.write("Excess Minutes Charge is: "+charge+" <br>");
  29. document.write("Total Charge is: "+total+" <br><br><br>");
  30. }
  31.  
  32.  
  33. else
  34. {
  35. document.write("Your Minutes Uses is: "+minutes+" <br>");
  36. document.write("Monthly Charges is: $"+monthly+".00<br><br><br>");
  37. }
  38.  
  39. }
  40. </script>
  41.  
  42. </body>
  43. </html>

Pleaseee helppp...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
poseoff is offline Offline
2 posts
since Jul 2004
Dec 4th, 2006
0

Re: Javascript - how to stop/halt/break this loop/script plss

Problem #1 if (minutes = 1) should be if (minutes ==1). You want to check the value of minutes using the == operator. I'm not sure what the = will do in JavaScript, but it is probably checking to see if minutes is a declared variable and returns true since it is.
Reputation Points: 45
Solved Threads: 28
Posting Whiz in Training
Dukane is offline Offline
282 posts
since Oct 2006
Dec 4th, 2006
0

Re: Javascript - how to stop/halt/break this loop/script plss

Click to Expand / Collapse  Quote originally posted by Dukane ...
Problem #1 if (minutes = 1) should be if (minutes ==1). You want to check the value of minutes using the == operator. I'm not sure what the = will do in JavaScript, but it is probably checking to see if minutes is a declared variable and returns true since it is.
he is right == checks equality and = equals the variable.



what code are u using for cancel button. have u used close() function.

wait, the <script> tag doesnt have all the necessary attributes also:
for javascript it must be
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">.....</script>
Last edited by vishesh; Dec 4th, 2006 at 9:35 am.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Dec 4th, 2006
0

Re: Javascript - how to stop/halt/break this loop/script plss

create a public/global variable called "stopped", set it to false when you start the loop.

inside the loop put:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. if(stopped){
  2. break;
  3. }
then when you press "cancel" set the stopped variable to true.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Dec 4th, 2006
0

Re: Javascript - how to stop/halt/break this loop/script plss

I took a look at your code, and made a few adjustments. Do you see why this works vs. your code?

javascript Syntax (Toggle Plain Text)
  1. <script type="text/javascript" lang="javascript">
  2. var i;
  3. var minutes = 0;
  4. var monthly;
  5. var excess;
  6. var charge;
  7. var total;
  8.  
  9. while (minutes != null) {
  10. minutes = prompt("Minutes Used:");
  11. alert("Your entry was: " + minutes);
  12.  
  13. monthly = 40;
  14. excess = minutes - 600;
  15. charge = excess * 0.49;
  16. total = monthly + charge;
  17.  
  18. if (minutes >= 600) {
  19. document.write("Your Minutes Uses is: " + minutes + "<br>");
  20. document.write("Excess Minutes Uses is: " + excess + "<br>");
  21. document.write("Monthly Charges is: $" + monthly + ".00<br>");
  22. document.write("Excess Minutes Charge is: " + charge + "<br>");
  23. document.write("Total Charge is: " + total + "<br><br><br>");
  24. } else {
  25. document.write("Your Minutes Use is: " + minutes + "<br>");
  26. document.write("Monthly Charge is: $" + monthly + ".00<br><br><br>");
  27. }
  28. }
  29. </script>
Reputation Points: 45
Solved Threads: 28
Posting Whiz in Training
Dukane is offline Offline
282 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Loading image to HTML
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Interpret HTTP (Content-Disposition) headers from an XMLHTTP object?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC