SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot irvin's Avatar
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to quit a script

    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

    Code:
    <html><head><title>Phone Cost</title></head>
    <body on>
    
    <script>
    var i
    var minutes
    var monthly
    var excess
    var charge
    var total
    
    while (minutes=1)
    
    {
    minutes = prompt("Minutes Used:");
    alert("Your entery was: "+minutes+"");
    
    monthly=40
    excess=minutes-600
    charge=excess*0.49
    total=monthly+charge
    
    	if(minutes>=600)
    	{
    		document.write("Your Minutes Uses is: "+minutes+" <br>");
    		document.write("Excess Minutes Uses is: "+excess+" <br>");
    		document.write("Monthly Charges is: $"+monthly+".00<br>");
    		document.write("Excess Minutes Charge is: "+charge+" <br>");
    		document.write("Total Charge is: "+total+" <br><br><br>");
    	}
    
    	
    	else
    	{
    		document.write("Your Minutes Uses is: "+minutes+" <br>");
    		document.write("Monthly Charges is: $"+monthly+".00<br><br><br>");
    	}
    	
    }
    </script>
    
    </body>
    </html>

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends on what browser your are using. In Opera the alert box has an option to stop the script.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    while (minutes=1)

    is assigning 1 to minutes and is therefore always true so there is no way out of the loop. Perhaps you meant

    while (minutes==1) or while (minutes!=1) to exit when a 1 is or isn't the value that was entered.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Zealot irvin's Avatar
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeFoster
    It depends on what browser your are using. In Opera the alert box has an option to stop the script.
    Hi, thank you for your reply.

    I'm using Internet Explorer v.6, I believe there's a way to quit this program while it's running after the user press the CANCEL button?

    For instance, within the WHILE loop, insert a ELSE IF statement, say...

    ELSE IF Cancel button is clicked THEN quit the whole program

    Do you know if anything could be done to the script to enable just that?
    I'm getting so desperate here.
    They will run and not grow weary,
    They will walk and not be faint.

  5. #5
    SitePoint Zealot irvin's Avatar
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall
    while (minutes=1)

    is assigning 1 to minutes and is therefore always true so there is no way out of the loop. Perhaps you meant

    while (minutes==1) or while (minutes!=1) to exit when a 1 is or isn't the value that was entered.
    Hi thanks for your reply. I tried both approach, minutes==1 and minutes!=1 but it still doesn't work, I mean even though after pressing the CANCEL button, it won't halt/quit the program.

    Any other idea pls?
    They will run and not grow weary,
    They will walk and not be faint.

  6. #6
    SitePoint Zealot irvin's Avatar
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone got any idea?

    Pls help somebody?

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    while(condition)
       do somethig
    if "condition" is always true, the loop will be executed forever and there is no way to break it, except closing the whole browser window. To keep interface responsive, you should use setInterval or setTimeout instead of loops.

  8. #8
    SitePoint Zealot irvin's Avatar
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    Code:
    while(condition)
       do somethig
    if "condition" is always true, the loop will be executed forever and there is no way to break it, except closing the whole browser window. To keep interface responsive, you should use setInterval or setTimeout instead of loops.
    Hi is there anyway to put a condition that asks/prompts the user to quit the program?

  9. #9
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, there is no such thing as "program" in javascript, so you cannot "quit" anything. To stop a loop you can use "break" statement:

    Code:
    while(condition) {
       if(!prompt("Continue?"))
           break;
       else
         do something
    }
    Keep in mind that the page won't be updated during the loop.

  10. #10
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The loop is because you wrote
    while (minutes=1) instead of while (minutes==1);
    which shouldn't do anything, since minutes is undefined.

  11. #11
    SitePoint Zealot irvin's Avatar
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem solved

    I used the if null condition, and it worked!

    Code:
    <html><head><title>Phone Cost</title></head>
    <body>
    
    <script>
    var i
    var minutes
    var monthly
    var excess
    var charge
    var total
    
    while (minutes=1)
    
    {
    minutes = prompt("Minutes Used:", null);
    alert("Your entry was: "+minutes+"");
    
    monthly=40
    excess=minutes-600
    charge=excess*0.49
    total=monthly+charge
    
    	if(minutes==null)
    	{
    		alert("Thank you for using this program");
    		document.write("*** End of Program ***");
    		break;
    	}
    
    	else if(minutes>=600)
    	{
    		document.write("Your Minutes Uses is: "+minutes+" <br>");
    		document.write("Excess Minutes Uses is: "+excess+" <br>");
    		document.write("Monthly Charges is: $"+monthly+".00<br>");
    		document.write("Excess Minutes Charge is: $"+charge+".00<br>");
    		document.write("Total Charge is: $"+total+".00<br><br><br>");
    	}
    
    	else
    	{
    		document.write("Your Minutes Uses is: "+minutes+" <br>");
    		document.write("Monthly Charges is: $"+monthly+".00<br><br><br>");
    	}
    }
    </script>
    
    </body>
    </html>
    Thanks everyone for your help.
    They will run and not grow weary,
    They will walk and not be faint.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •