User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 397,809 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,524 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 4451 | Replies: 4
Reply
Join Date: Jul 2004
Posts: 2
Reputation: poseoff is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
poseoff poseoff is offline Offline
Newbie Poster

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

  #1  
Dec 3rd, 2006
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

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

Pleaseee helppp...
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Location: NY
Posts: 195
Reputation: Dukane is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 15
Dukane's Avatar
Dukane Dukane is offline Offline
Junior Poster

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

  #2  
Dec 4th, 2006
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.
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 1,289
Reputation: vishesh is on a distinguished road 
Rep Power: 4
Solved Threads: 32
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

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

  #3  
Dec 4th, 2006
Originally Posted by Dukane View Post
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
<script type="text/javascript">.....</script>
Last edited by vishesh : Dec 4th, 2006 at 8:35 am.
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 47
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

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

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

inside the loop put:
if(stopped){
  break;
}
then when you press "cancel" set the stopped variable to true.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Oct 2006
Location: NY
Posts: 195
Reputation: Dukane is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 15
Dukane's Avatar
Dukane Dukane is offline Offline
Junior Poster

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

  #5  
Dec 4th, 2006
I took a look at your code, and made a few adjustments. Do you see why this works vs. your code?

  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>
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 6:19 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC