LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-22-2006, 09:17 PM   #1
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
How do I stop a JavaScript program?


I wrote a program in JavaScript, embedded in a web-page, to process a series of numbers. The program should terminate when the startNum parameter fed to the function which outputs the results reaches 9999. However, this is not happening. The relevant bit of code (from the page body) is:
Code:
<script type="text/JavaScript">
function outputResult(startNum,Iterations,finNum,functions)
{
	if (startNum != 9999)
	{
		//several document.writeln statements snipped...

		generateNumber(startNum) 
	}
	else
	{
		alert("this should be the end")
		flag = 0
	}
}
if (flag == 1)
{
	var startValue = prompt("Enter starting number.")

	//irrelevant error checking snipped...

        generateNumber(startValue)
}
</script>
generateNumber() is the function (located in the page head) which starts a new pass through the program. The block beginning with if (flag == 1) initiates the program.

The problem is that, although the else... block of the outputResult() function is executed (the alert box I inserted, in an attempt to debug it, does appear), the program then continues. I cannot see why this happens.

Is there something fundamental that I'm not understanding about JavaScript? Do I need to add an explicit command to exit the program?

Hoping someone can help...

Thanks,
Rob.
 
Old 04-22-2006, 09:27 PM   #2
ataraxia
Member
 
Registered: Apr 2006
Location: Pittsburgh
Distribution: Debian Sid AMD64
Posts: 296

Rep: Reputation: 30
I think that Javascript is scoped such that "flag" in the function isn't the same variable as "flag" in the outer program.
 
Old 04-22-2006, 09:33 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Original Poster
Rep: Reputation: 97
I did declare flag outside the function, however (though in the script block in the page head), which I thought made it a global variable . Also, the bottom if block doesn't seem to be implicated, as the prompt box doesn't appear (possibly should have said that earlier).

Thanks,
Rob
 
Old 04-22-2006, 09:46 PM   #4
ataraxia
Member
 
Registered: Apr 2006
Location: Pittsburgh
Distribution: Debian Sid AMD64
Posts: 296

Rep: Reputation: 30
How does outputResult actually get called? I only see generateNumber calls.

(I barely know Javascript, so I probably won't be much more help.)
 
Old 04-23-2006, 05:05 AM   #5
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Original Poster
Rep: Reputation: 97
Quote:
Originally Posted by ataraxia
How does outputResult actually get called?
It is called from a function (actually, one of two functions) located in the page head. It is definitely being called correctly, as otherwise there would be no output, and the else block of outputResult does get called when 9999 is reached.

The problem is that the program then continues to run, despite the fact that there are no further function calls, and no return statement. I am wondering whether JavaScript is assuming a return by default (so continuing to run from the next statement in the function that called it), and if I need to add a command to override that.

Rob.

Edited to add:
Quote:
I am wondering whether JavaScript is assuming a return by default...
I've just confirmed that this is what is happenning - I added an alert immediately after the call to outputResult, which displayed as I anticipated. So: I now know why, but not how to stop it.

I've realised that I was thinking of calling a function as being equivalent to using GoTo in BASIC. All the other functions were calling others, or returning values, so I was thinking that as this one didn't, the program would just stop .

Does JavaScript have an end/exit/quit method?

Last edited by Robhogg; 04-23-2006 at 06:13 AM.
 
Old 04-23-2006, 07:37 AM   #6
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Original Poster
Rep: Reputation: 97
Well, I've found a solution, though I don't feel it's particularly elegant. I wrapped
Code:
if (flag != 0) 
{
...
}
around the code in the (recursive) generateNumber() function. This does stop further numbers being processed after the limit is reached, but I'd still be interested to know whether there's a JavaScript equivalent of end() or die().

Thanks,
Rob
 
Old 04-23-2006, 09:12 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Such a feature would be very odd, as ending the runtime would mean ending all javascript handlers in the browser, including those not belonging to the caller.

Just like System.exit() is forbidden is Java when the JVM is running in a browser, exitting the Javascript runtime is unwanted, in my opinion.

Calling a function is certainly not like a basic goto, as you figured out, but most like a gosub.
 
Old 04-23-2006, 09:54 AM   #8
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Original Poster
Rep: Reputation: 97
Quote:
Originally Posted by jlliagre
Such a feature would be very odd, as ending the runtime would mean ending all javascript handlers in the browser, including those not belonging to the caller.
But wouldn't it be possible for a function to be added to stop the running of a particular group of functions, possibly using a script tag id?

Anyway, thanks for the answer. This was my first attempt to write a program using several recursive functions (to examine Kaprekar's Operation). Good learning experience. Among other things, I learnt that JavaScript is not really suited to a task like this (it crashed out after processing ~200 numbers each time, due to "Too many recursions"). I may rewrite it in C++, but it's a shame as JavaScript/HTML make it very easy to format the results nicely.

Thanks,
Rob
 
Old 04-23-2006, 01:55 PM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Recursive programming, whatever the language, often lead to disappointments.

At least Javascript was smart enough to tell you the recursion issue, C or C++ would usually badly crash in such a situation ...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to stop program looping? twirl Programming 5 10-10-2005 06:05 AM
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 09:36 AM
How do I stop a program that has crashed? Fe98 Linux - Newbie 4 12-08-2003 07:09 AM
A small program to stop iptables satimis Linux - Newbie 6 09-15-2003 12:04 AM
How do i stop a locked program? glenn69 Linux - Newbie 1 09-01-2003 11:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration