Example produces infinite loop - my solution fails

R

Richard

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 previewed the example.

On the off chance that example exposed a weakness in HTML-Kit, I ran
the example in IE7, which also hung.

The example seems to start an infinite loop which the user should be
able to stop by clicking a button. I thought the problem might be
solved by adding a "start" button. I did that, as shown in the code
below. However, when I click the Start button, I get MegBox
announcing "Object doesn't support this action" with reference to the
button's defining source-code line. Any ideas?

My machine environment is shown beneath the example.

Thanks in Advance,
Richard

<HTML>

<HEAD>

<SCRIPT LANGUAGE="JavaScript"><!--
var running = true;
var timer;

function loop() {
while (running) timer = window.setTimeout('loop()',100) ; // loop
every 100 milliseconds
}

function stop() {
alert('stopping');
window.clearTimeout(timer);
running = false;
}
//--></SCRIPT>

</HEAD>

<BODY>

<FORM>
<INPUT TYPE="BUTTON" VALUE="Stop" onClick="stop()">
</FORM>

<SCRIPT LANGUAGE="JavaScript"><!--
loop();
//--></SCRIPT>

</HTML>

Software: WinXP-Pro/SP2, Ruby 1.8.2-15, Rails 1.1.6,
Gem 0.9, MySQL 5.0.27-nt, SciTE 1.72, Nero Ultra 7.9.6.0,
FireFox 2.0.0.1, IE 7.0, OE 6.0, MS Office 2003 SP2,
Java JVM 1.5.0_11-b03, Apache Tomcat/5.5.12,
AVG-Free 7.5.430, Prevx1 2.0.2.23, Startup Cop Pro 2.03
 
L

Lee

Richard said:
Hi All,

I copied a script example from http://www.irt.org/script/640.htm into
a local .html file.

function loop() {
while (running) timer = window.setTimeout('loop()',100) ; // loop
every 100 milliseconds
}

That doesn't loop every 100 milliseconds.
setTimeout() doesn't insert any sort of delay into your code.
It simply schedules something to happen after the specified
delay.

In this case, the loop spins as fast as it can, and on each
pass it schedules another instance of loop() to start after
1/10th of a second. You probably want something more like:

function loop() {
if (running) {
// do something useful, one would hope.
timer = setTimeout("loop()",100); // loop every 100 ms.
}
}


--
 
L

Lee

Richard said:
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 previewed the example.

I finally looked at that example, and it's pretty bad.
It doesn't do what the author seemed to intend, at all.
The following is better:

<html>
<head>

<script type="text/javascript">
var running = true;
var timer;

function loop() {
if (running) {
document.forms[0].output.value=1+parseInt(document.forms[0].output.value,10);
timer = window.setTimeout('loop()',100) ; // loop every 100 milliseconds
}
}

function stop() {
running = false;
window.clearTimeout(timer);
alert('Stopped');
}
</script>

</head>

<body>

<form>
<input type="input" name="output" value="0">
<input type="button" value="Stop" onclick="stop()">
</form>

<script type="text/javascript">
loop();
</script>

</html>


--
 
R

Richard

Hi Lee,

Thanks for your recommendations. I very much appreciate such a rapid
answer and then a thoughtful critique. I'll work on it tomorrow;
it's 11 p.m. my time, so even though my spirit is willing to tackle it
now, the flesh is weak. :)

Best wishes,
Richard

Richard said:


I copied a script example fromhttp://www.irt.org/script/640.htminto
a local .html file. I opened that file first in HTML-kit, which hung
(in an infinite loop, I think) when I previewed the example.

I finally looked at that example, and it's pretty bad.
It doesn't do what the author seemed to intend, at all.
The following is better:

<html>
<head>

<script type="text/javascript">
var running = true;
var timer;

function loop() {
if (running) {
document.forms[0].output.value=1+parseInt(document.forms[0].output.value,10);
timer = window.setTimeout('loop()',100) ; // loop every 100 milliseconds
}

}

function stop() {
running = false;
window.clearTimeout(timer);
alert('Stopped');}

</script>

</head>

<body>

<form>
<input type="input" name="output" value="0">
<input type="button" value="Stop" onclick="stop()">
</form>

<script type="text/javascript">
loop();
</script>

</html>

--
 
R

RobG


Please don't top-post, reply below trimmed quotes.
Thanks for your recommendations. I very much appreciate such a rapid
answer and then a thoughtful critique.

If you intend to run something at a regular interval, you should
consider setInterval:

<script type="text/javascript">

var timer = (function(){
var timeoutRef;
function foo(){
/* do something useful... */
document.getElementById('xx').innerHTML =
(new Date()).getMilliseconds();
}
return {
start: function(lag){
timeoutRef = setInterval(function(){foo();},lag);
},
stop: function(){
if (timeoutRef) clearTimeout(timeoutRef);
}
}
})();

</script>

<button onclick="timer.start(100);">Start</button>
<button onclick="timer.stop();">Stop</button>
<p id="xx"></p>
 
R

Richard

Please don't top-post, reply below trimmed quotes.


If you intend to run something at a regular interval, you should
consider setInterval:

<script type="text/javascript">

var timer = (function(){
var timeoutRef;
function foo(){
/* do something useful... */
document.getElementById('xx').innerHTML =
(new Date()).getMilliseconds();
}
return {
start: function(lag){
timeoutRef = setInterval(function(){foo();},lag);
},
stop: function(){
if (timeoutRef) clearTimeout(timeoutRef);
}
}
})();

</script>

<button onclick="timer.start(100);">Start</button>
<button onclick="timer.stop();">Stop</button>
<p id="xx"></p>

Hi Rob,

Thanks for your advice. Your example worked fine. I just Googled for
innerHTML and finally got a handle on that. I look forward to using
your advice and Lee's in the future.
Please don't top-post, reply below trimmed quotes.

What difference does it makes. I assume that one opens a posted
item, one wishes to read that item's content first and then,
possibly, look beneath the item to refer to preceding items.

Best wishes,
Richard
 
R

Richard

Hi Lee,

Thanks for your recommendations. I very much appreciate such a rapid
answer and then a thoughtful critique. I'll work on it tomorrow;
it's 11 p.m. my time, so even though my spirit is willing to tackle it
now, the flesh is weak. :)

Best wishes,
Richard

Richard said:
I finally looked at that example, and it's pretty bad.
It doesn't do what the author seemed to intend, at all.
The following is better:

<script type="text/javascript">
var running = true;
var timer;
function loop() {
if (running) {
document.forms[0].output.value=1+parseInt(document.forms[0].output.value,10);
timer = window.setTimeout('loop()',100) ; // loop every 100 milliseconds
}

function stop() {
running = false;
window.clearTimeout(timer);
alert('Stopped');}
</script>

<form>
<input type="input" name="output" value="0">
<input type="button" value="Stop" onclick="stop()">
</form>
<script type="text/javascript">
loop();
</script>

--

Hi Lee,

My reply to Rob applies to you equally:

Hi Rob,

Thanks for your advice. Your example worked fine. I just Googled for
innerHTML and finally got a handle on that. I look forward to using
your advice and Lee's in the future.

Best wishes,
Richard
 
L

Lee

Richard said:
What difference does it makes. I assume that one opens a posted
item, one wishes to read that item's content first and then,
possibly, look beneath the item to refer to preceding items.

It makes a big difference once a thread begins to grow.
If you had top-posted that last response, I wouldn't have been
able to trim the irrelevant lines as easily and if I had answered
you down here, it would have been really hard for the next person
who comes along (possibly with some very good advice) to figure
out what we're talking about.

Some people prefer top-posting, most seem to prefer bottom-posting.
The most important thing is that everybody stick to one convention or
the other, and in this newsgroup, the convention is to bottom-post.


--
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
glegroups.com>, Mon, 2 Jul 2007 12:54:27, Richard <RichardDummyMailbox58
(e-mail address removed)> posted:
What difference does it makes. I assume that one opens a posted
item, one wishes to read that item's content first and then,
possibly, look beneath the item to refer to preceding items.


Then you are wrong. And don't over-quote either. Read the newsgroup
FAQ.

Please don't sign yourself just "Richard"; we already have one.
 
R

Richard

In comp.lang.javascript message <[email protected]
glegroups.com>, Mon, 2 Jul 2007 12:54:27, Richard <RichardDummyMailbox58
(e-mail address removed)> posted:





Then you are wrong. And don't over-quote either. Read the newsgroup
FAQ.

Please don't sign yourself just "Richard"; we already have one.

--
(c) John Stockton, Surrey, UK. [email protected] Turnpike v6.05 IE 6
FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Then you are wrong.

Well, it doesn't sound like I'm flat-out wrong if Lee is to be
believed. He says some newsgroups use the style I like. He says [it's
merely that] this newsgroup (and some others) have opted for the style
you advocate.
And don't over-quote either.

What does "over-quote" mean in the context of my submissions?
Please don't sign yourself just "Richard"; we already have one.

In my experience, newsgroups that care about unique "handles" have
declined to allow me to use a pre-existing handle when I subscribed.
This newsgroup declined to indicate that a subscriber "Richard"
existed, so this duplication doesn't seem to matter. Besides, with
thousands of subscribers, it appears to mre unlikely that the other
"Richard" and I will post to the same thread. Don't you agree?

Regards,
Richard
 
R

Richard

Richard said:





It makes a big difference once a thread begins to grow.
If you had top-posted that last response, I wouldn't have been
able to trim the irrelevant lines as easily and if I had answered
you down here, it would have been really hard for the next person
who comes along (possibly with some very good advice) to figure
out what we're talking about.

Some people prefer top-posting, most seem to prefer bottom-posting.
The most important thing is that everybody stick to one convention or
the other, and in this newsgroup, the convention is to bottom-post.

--

OK, Thanks.
 
R

rf

And don't over-quote either.

What does "over-quote" mean in the context of my submissions?

Get rid of the stuff that does not concern your post. Like I just did.
Besides, with
thousands of subscribers,

Nobody "subscribes" to this newsgroup. It's simply lying around waiting for
you to look at it. The fact that you drifted in here from the dreaded google
groups is irrelevant.
it appears to mre unlikely that the other
"Richard" and I will post to the same thread. Don't you agree?

Nope :)

But IMHO you can call yourself what ever your want. Crikey, even use your
real name if you wish.
 
R

Richard

Get rid of the stuff that does not concern your post. Like I just did.


Nobody "subscribes" to this newsgroup. It's simply lying around waiting for
you to look at it. The fact that you drifted in here from the dreaded google
groups is irrelevant.


Nope :)

But IMHO you can call yourself what ever your want. Crikey, even use your
real name if you wish.

Hi Richard,
Get rid of the stuff that does not concern your post. Like I just did.

That makes sense. I asked because I didn't think I quoted excessively
in my previous posts.
Nobody "subscribes" to this newsgroup. It's simply lying around waiting for
you to look at it. The fact that you drifted in here from the dreaded
google
groups is irrelevant.

Well, it seems from the Google-Groups perspective, AFAIK, one must
subscribe to any newsgroups one wishes to post to. I used to access
visit newsgroups using Outlook Express, which I liked a lot. But I
find Google-Groups for a couple of things, particularly the I can flag
the threads I start and then see a list of all my "favorite" (i.e.,
flagged) threads in a single list, irrespective of the actual
newsgroups where they're posted. I find it to be an excellent tool.
But I'm not an evangelist: "to each his own" is my motto.
But IMHO you can call yourself what ever your want.

Thanks for that.

Best wishes,
Richard
 
R

rf

Richard said:
That makes sense. I asked because I didn't think I quoted excessively
in my previous posts.

Yet you mucked it up totally in this one. Please learn how to use google
groups properly or, preferably, abandon it.
Well, it seems from the Google-Groups perspective,

From this perspective google groups is a pain in the arse. An increasing
number of people have plonked the entire lot.
http://blinkynet.net/comp/uip5.html

Go back to outlook express or some better newsreader.
 
E

Evertjan.

Lee wrote on 03 jul 2007 in comp.lang.javascript:
..., and in this newsgroup, the convention is to bottom-post.

It is not, Lee, sparce interunderposting is the convention here.

If the convention were bottomposting,
topposting would not be such a dreadful alternative
as it is now.
 
R

Richard

Yet you mucked it up totally in this one. Please learn how to use google
groups properly or, preferably, abandon it.


From this perspective google groups is a pain in the arse. An increasing
number of people have plonked the entire lot.http://blinkynet.net/comp/uip5.html

Go back to outlook express or some better newsreader.

I read the BlinkyNet article you referenced. One person posted there
that Outlook Express was just as bad as GG. As a matter of fact, I
switched to GG because some person claimed that OE, which I was using
at the time, was so inferior to GG.

The main thrust of the article seems to be that GG facilitates amateur
access to UseNet and some (most?) of these amateurs post stupid
questions, perhaps fail to acknowledge responses and, above all
perhaps, fail to indicate whether proffered help proved useful.

I'm a retired computer consultant. I've started programming (on paper
for a computer at Princeton) in the Fifties. I started programming
professionally in the early Sixties. I've been using the 'Net
"forever" it seems and newsgroups for a decade(s?), receiving
invariably friendly and positive responses, barring a few exceptions
like this thread.

I mention this only because as a retiree, I can indulge myself in
following a wide number of interests, which leads me to visit a number
of newsgroups. GG facilitates that by allowing me to flag each post
so that I can get a display of all the threads I'm current involved
in, along with their status. Threads that receive new posts pop up to
the top of that list, facilitating prompt response on my part. Does
UseNet provide similar functionality?

You say "Yet you mucked it up totally in this one."

AFAIK, the only offenses I allegedly committed on this thread was top-
posting and excessive quoting. Yet in my previous post, I thought I
"bottom posted" and I quoted with ">" punctuation a minor fragment of
of your antecedent post. So what did I do to "muck it up totally".

Lastly, I'm not trying to start a "religious war." I'm merely trying
to see if you and I can have a meeting of the minds.

Best wishes,
Richard
 
R

Richard

Lee wrote on 03 jul 2007 in comp.lang.javascript:


It is not, Lee, sparce interunderposting is the convention here.

If the convention were bottomposting,
topposting would not be such a dreadful alternative
as it is now.

Hi Evertjan,

Is there an online write-up on this issue?
 
E

Evertjan.

Richard wrote on 04 jul 2007 in comp.lang.javascript:

[please do not quote signatures]
Is there an online write-up on this issue?

What is that 'an online write-up', Richard?

Do you mean a web page?
 
R

Richard

Richard wrote on 04 jul 2007 in comp.lang.javascript:

[please do not quote signatures]
Is there an online write-up on this issue?

What is that 'an online write-up', Richard?

Do you mean a web page?

Hi Evertjan,
Is there an online write-up on this issue?

Yes. I Googled for "interunderposting" and got no hits.
[please do not quote signatures]

In my previous post, I "underposted," as advocated by several people
on this thread. That is, in Google Groups, I clicked the "More
options" link which Google supplies along with the content of your
post. In the resulting menu, I clicked the "Reply" link. That
opened a new message pane containing your message decorated with
leading ">" signs.

I added my response below the decorated text, expecting Google to
merely present a hyperlink to your message. As I see, GG did *not*
condense your message. Looking back, I see that if I underpost, then
GG does present a "- Show quoted text -" hyperlink which toggles the
content of the message to which I'm complying.

However, if I bottom post, then GG puts only a couple of lines after
my new content:

=== Example


- Show quoted text -
=== End of example

A good example is the thread http://groups.google.com/group/DotNetDevelopment/browse_frm/thread/80b79aa5d8bf8b1e
It shows that the growing thread is preserved with each subsequent
post. But that growing thread is unobtrusive, because it's
represented by an Ajax type link. Pretty neat, eh?

Hopefully, this post of mine will work that way, too. What do you
think?

Best wishes,
Richard
 
R

Richard

Sorry, Evertjan,

I meant to say I was responding "Yes." to the following line.
Do you mean a web page?

I trust you were not confused by my sloppiness, for which I
apologize.

Best wishes,
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top