且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

示例产生无限循环 - 我的解决方案失败

更新时间:2023-02-16 21:31:50

Richard说:

>
大家好,

我从 http://www.irt.org/script/640.htm 进入
一个本地的.html文件。


> function loop(){

while(running)timer = window.setTimeout(''loop() '',100); //循环
每100毫秒
}



这不会每100毫秒循环一次。

setTimeout ()不会在代码中插入任何类型的延迟。

它只是安排在指定的

延迟后发生的事情。


在这种情况下,循环尽可能快地旋转,并且在每个
传递上它调度另一个loop()实例,以便在

1 /之后启动十分之一秒。你可能想要更像的东西:


函数循环(){

if(running){

//做一些有用的事情人们希望。

timer = setTimeout(" loop()",100); //每100毫秒循环一次。

}

}

-

>

Richard说:

>
6月30日晚上11:49,RobG< r ... @ iinet.net。 auwrote:


>请不要顶尖,回复以下修剪报价。


它有什么不同。我假设一个人打开了一个已发布的项目,一个人希望先读取该项目的内容,然后可能会在项目下方查看前面的项目。



一旦线程开始增长就会产生很大的不同。

如果你最后一次回复,我就不会''已经

能够轻松修剪不相关的线条,如果我已经回答了这个问题,那么对于下一个人来说这真的很难

谁来(可能有一些非常好的建议)来计算我们正在讨论的



有些人更喜欢top -posting,大多数人似乎更喜欢底部发布。

最重要的是每个人都坚持一个约定或者另一个是b $ b,而在这个新闻组中,约定是到底部。

-


在comp.lang.javascript消息< 11 ****** ***************@c77g2000hse.goo

glegroups.com>,星期一,2007年7月2日12:54:27,Richard< RichardDummyMailbox58 > 40*@USComputerGurus.com 发布:

>

>请不要顶尖,回复以下修剪报价。


它有什么不同。我假设一个人打开了一个已发布的项目,一个人希望先读取该项目的内容,然后可能会在项目下方查看前面的项目。



然后你错了。而且也不要过度引用。阅读新闻组

常见问题。


请不要只签署理查德;我们已经有了。


-

(c)John Stockton,英国萨里。 ?@merlyn.demon.co.uk Turnpike v6.05 IE 6

news:comp.lang.javascript FAQ< URL:http://www.jibbering.com/faq/index.html&gt ;。

< URL:http://www.merlyn.demon.co.uk/js-index.htmjscr数学,日期,来源。

< URL :http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/& c,常见问题项目,链接。


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

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


Richard said:
>
On Jun 30, 11:49 pm, RobG <r...@iinet.net.auwrote:

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

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


In comp.lang.javascript message <11*********************@c77g2000hse.goo
glegroups.com>, Mon, 2 Jul 2007 12:54:27, Richard <RichardDummyMailbox58
40*@USComputerGurus.composted:
>
>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.


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. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.