Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why JavaScript doesn't have sleep?!!

1 view
Skip to first unread message

RC

unread,
Mar 16, 2004, 10:19:01 AM3/16/04
to
The Subject says its all.
Most of computer languages and script
languages have sleep function, except JavaScript.

The setTimeout function in JavaScript is a very
stupid function!

Evertjan.

unread,
Mar 16, 2004, 10:48:43 AM3/16/04
to
RC wrote on 16 mrt 2004 in comp.lang.javascript:
> The Subject says its all.
> Most of computer languages and script
> languages have sleep function, except JavaScript.

Clientside scripts are asynchronous.



> The setTimeout function in JavaScript is a very
> stupid function!

If you say so.

My personal meaning is that you should stop the idea of linear programming,
the "goto" concept, where a single line of happenings all wait for the last
to complete.

A asynchronoud language has far more to offer:
Simultaneous and event driven execution.

The problem is not the language but the silly fixed preconceived ideas WE
"older" programmers are brought up in.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Mark Preston

unread,
Mar 15, 2004, 12:32:16 PM3/15/04
to
RC wrote:

Given that Javascript is for an ASYNCHRONOUS client, what on Earth use
would a "sleep" command be?

Lee

unread,
Mar 16, 2004, 12:14:00 PM3/16/04
to


The lack of a sleep() functionality is not a feature of
Javascript, but of the web browser environment. When you
use Javascript in other environments, they often provide
some sort of sleep() function.

Web browsers are event-driven systems. You should never
make an event-drive system sleep. It must always be able
to respond to user-initiated events. Web-browsers are
certainly not the only event-driven systems that don't
provide, or at least strongly discourage the use of, a
sleep() function.

Another lesson that you may learn from this experience is
that when you post to a language newsgroup, complaining that
some feature of that language is "stupid", you generally
come across looking foolish. It would be better to just ask
for an explanation, without offering your opinion.

Richard Cornford

unread,
Mar 16, 2004, 1:17:53 PM3/16/04
to
RC wrote:
> The Subject says its all.

It is traditional on Usenet not to assume that the reader has access to
the subject header.

> Most of computer languages and script
> languages have sleep function, except JavaScript.

ECMAScript leaves that up to the host environment. But it is not
designed to multithread so there could be no expectation of anything
else being able to happen while it was sleeping, at least in terms of
the scripting language. The host environment, on the other hand, is
entirely capable of multithreading so it could be doing something else
while the javascript implementation was suspended. And Windows Scripting
Host, as a host environment, does provide a Sleep method that can be
called from JScript.

Web browsers are event driven and need to have their javascript
implementation available to process events. A sleep method would be a
bad idea in that context and so they don't provide one. Most provide a
mechanism for scheduling the execution of code with setTimeout/Interval
but under circumstances where sleep might otherwise be indicated they
tend to provide an event driven mechanism instead.

Javascript is very flexible, but it still has to be written in a way
that is suited to the environment in which it is used.

> The setTimeout function in JavaScript is a very
> stupid function!

setTimeout is not part of ECMAScript, it is a method provided by most
web browsers as part of their host environment.

When there is no option to provide an event handler for a task the
ability to execute arbitrary code after a defined interval is the only
reasonable alternative in an environment that should not be suspended.
Low-resolution timers and the inability of setTimeout to interrupt
executing code (because of the lack of multithreading) make it less than
precise but an unqualified "very stupid" doesn't seem appropriate.

Richard.


RC

unread,
Mar 16, 2004, 3:25:43 PM3/16/04
to

Lee wrote:

>
> Web browsers are event-driven systems. You should never
> make an event-drive system sleep.

Why? If I want to created a digital counter, update
the display every second, then what? I can do this
with Java applet. I don't want to used Java applet,
because most browser doesn't support above Java 1.2.

Here is one of example from my application

http://dipper.nws.noaa.gov/nexhads/jsp/dipper/prepareDCPChart.jsp?nesdis_id=FA6362DE&nwsli=TWRN5&pe_code=MT

You will see "Please wait ..." Instead display a static html file,
I would like use a counter count how many seconds or minutes
(without use Java applet). Because the network sometime fast,
sometime slow. With a sleep function in JavaScript will be
easy for me. The setTimeout function just can't get the work
done. Is there a "resetTimeout" function in JavaScript
I haven't known, yet.

Brian Genisio

unread,
Mar 16, 2004, 3:27:37 PM3/16/04
to
RC wrote:
> Most of computer languages and script
> languages have sleep function, except JavaScript.

I disagree with this statement. Most computer languages and script
languages do _not_ have a sleep function. Only languages that are
designed to be portable have sleep functions. I know ADA has one, and I
am pretty sure that Java has one, but other than that, I cannot think of
another one off the top of my head. (I am sure there are more)

C and C++ do not have sleep functions. The Operating System environment
they execute in provide the sleep functions. It is entirely possible to
run C/C++ code on a system without sleep. I have seen it before :)

So, the question might be, why dont the browsers provide a sleep
function to JavaScript? I can answer that, but I would be repeating
what everyone else has said in previous resposnes.

> The setTimeout function in JavaScript is a very
> stupid function!

I also disagree with this statement, but I would be willing to hear why
you think it is stupid. Do you mean it is not flexible enough (not
enough parameters)? Or do you mean the time resolution is not percise
enough?

My guess is that there is a good solution for whatever you want to do,
and you have not found it yet. You are probably frustrated, and that is
OK. I remember a time when I felt the same thing. (Wow, I sound
patronizing... I dont mean to)

Anyways, I am suggesting that you post what you want to accomplish with
sleep, and we will very likely find a good solution for you, as far as
the Browser can support.

See ya,
Brian

Lasse Reichstein Nielsen

unread,
Mar 16, 2004, 3:48:51 PM3/16/04
to
RC <raymon...@noaa.gov> writes:

> Why? If I want to created a digital counter, update
> the display every second, then what?

var tickId = setInterval(updateCounter,1000);
or
var tickId = setInterval("updateCounter();",1000);

> I can do this with Java applet. I don't want to used Java applet,
> because most browser doesn't support above Java 1.2.

It's much easier with Javascript too.

> With a sleep function in JavaScript will be
> easy for me.

I don't think a busy loop with sleep inside will be better
than using setTimeout or setInterval.

> The setTimeout function just can't get the work
> done.

Sure it can.

> Is there a "resetTimeout" function in JavaScript
> I haven't known, yet.

clearInterval(tickId);

Ditto for setInterval/clearInterval, try reading about it (before
complaining that it's stupid):
<URL:http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/window.html#1203758>

/L
--
Lasse Reichstein Nielsen - l...@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Brian Genisio

unread,
Mar 16, 2004, 3:36:31 PM3/16/04
to
RC wrote:

>
>
> Lee wrote:
>
>>
>> Web browsers are event-driven systems. You should never
>> make an event-drive system sleep.
>
>
> Why? If I want to created a digital counter, update
> the display every second, then what?

Because sleeping in an event driven system will cause all events in the
system to hang. If you sleep for 1 second, the browser will be
completely unresponsive for the entire second. All mouse movements will
be queued up, waiting for you to return from your script.

This is the case in most GUI based applications. For instance, you
never want to sleep in an MFC application, unless you do it in a
separate thread. Sleeping in an MFC application will have the same
effect... a hung GUI.

Is this the site you are having troubles with?

>
> You will see "Please wait ..." Instead display a static html file,
> I would like use a counter count how many seconds or minutes
> (without use Java applet). Because the network sometime fast,
> sometime slow. With a sleep function in JavaScript will be
> easy for me. The setTimeout function just can't get the work
> done.

Hmmm.. Why not? This seems like a perfect application of setTimeout. 1
second updates whould happen with setTimeout. When your function gets
called, after 1 second, you call setTimeout again, to call itself.

There is also setInterval on the newer browsers, that will update every
interval. This would also do what you want.

>Is there a "resetTimeout" function in JavaScript
> I haven't known, yet.
>

setTimeout returns an ID for the given timeout. You can clear it using
clearTimeout. Is this what you are looking for?

Brian

Jim Ley

unread,
Mar 16, 2004, 5:38:48 PM3/16/04
to
On Tue, 16 Mar 2004 10:19:01 -0500, RC <raymon...@noaa.gov> wrote:

>The Subject says its all.
>Most of computer languages and script
>languages have sleep function, except JavaScript.

An event driven programming paradigm is ideal for User Interfaces,
since the web is basically a user interface (in that it's something a
user interacts wth and not just performs calculations) then the most
logical paradigm is the event driven one.

http://en.wikipedia.org/wiki/Event-driven_programming

(might be worth a <FAQENTRY> )

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Ivan Marsh

unread,
Mar 16, 2004, 6:28:57 PM3/16/04
to

Because the script is executed as it's downloaded, what would be the
purpose of a sleep function?

setTimeout should work just fine unless you don't know how to write a
function.


--
i.m.
The USA Patriot Act is the most unpatriotic act in American history.

Craig Richardson

unread,
Mar 16, 2004, 10:22:36 PM3/16/04
to
On Tue, 16 Mar 2004 21:48:51 +0100, Lasse Reichstein Nielsen
<l...@hotpop.com> wrote:

>I don't think a busy loop with sleep inside will be better
>than using setTimeout or setInterval.

As a general rule, this one is pretty good. Not even limited to
javascript, but any language that allows timers.

--Craig


--
Craig Richardson (Homepage <http://crichard-tacoma.home.att.net>)
"Rapid prototyping has enormous, obvious advantages beyond destroying
humanity as we know it" -Michal Ash in rec.arts.sf.written [04/1/26]

Richard Cornford

unread,
Mar 17, 2004, 2:21:17 AM3/17/04
to
Jim Ley wrote:
<snip>

> An event driven programming paradigm is ideal for User Interfaces,
<snip>

Twice in one week certainly makes it look frequent, I will add it to my
to do list.

Incidentally, I have done the FAQ update (this morning), the on-line
version is working so if today's FAQ post is version 8.0 I will assume
that everything worked as planned.

Richard.


Brian Genisio

unread,
Mar 17, 2004, 7:27:21 AM3/17/04
to
Craig Richardson wrote:
> On Tue, 16 Mar 2004 21:48:51 +0100, Lasse Reichstein Nielsen
> <l...@hotpop.com> wrote:
>
>
>>I don't think a busy loop with sleep inside will be better
>>than using setTimeout or setInterval.
>
>
> As a general rule, this one is pretty good. Not even limited to
> javascript, but any language that allows timers.
>
> --Craig
>
>

Absolutely. Sleeping implies blocking, which is rarely necessary,
especially in an event-driven system.

Brian

0 new messages