473,785 Members | 2,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is setTimeout() asynchronous?

VA
Suppose I have

setTimeout(foo( ),n);
long_running_fu nction();

Would foo() be executed even when long_running_fu nction() is still
executing? In other words, does the setTimeout() pre-empt anything
else?

[The reason I ask is that I would like to show some feedback to the
user if my long running function takes more than n milliseconds]

Thanks

Oct 23 '05 #1
8 15102
VA wrote:
Suppose I have

setTimeout(foo( ),n);
long_running_fu nction();

Would foo() be executed even when long_running_fu nction() is still
executing? In other words, does the setTimeout() pre-empt anything
else?
Nope. JavaScript is single-threaded, so the return value of foo() won't
be executed until after long_running_fu nction finishes.
[The reason I ask is that I would like to show some feedback to the
user if my long running function takes more than n milliseconds]


Think of it as co-operative multitasking. You'll have to simulate a
yield() in order to allow other code to be executed.

For instance, you can have long_running_fu nction either use global
variables or a closure of some sort to keep track of where it is when
it's about to 'yield', then return() after a
setTimeout(long _running_functi on,1), and have it pick up where it left
off.

Of course, you may also need to ensure that whatever fires off
long_running_fu nction doesn't fire again while it's still running--
since yielding could theoretically make that possible. But that depends
on what it's doing, since of course... it'd pick up where the original
left off, and the original wouldn't continue running until the second
had yielded-- and it would pick up where the second had left off.

Also, long_running_fu nction's return value wouldn't be meaninful, since
it might not finish by the time it return()s.

Just an idea. I'm sure there're better ones out there.

Oct 23 '05 #2
VA said the following on 10/22/2005 11:37 PM:
Suppose I have

setTimeout(foo( ),n);
long_running_fu nction();

Would foo() be executed even when long_running_fu nction() is still
executing?
It would be put in line to be executed if long_running_fu nction() is
still running after n milliseconds. But foo() would not be executed
until the previous function finished.
In other words, does the setTimeout() pre-empt anything else?
No.
[The reason I ask is that I would like to show some feedback to the
user if my long running function takes more than n milliseconds]


Break long running function into sub routines and set a timeout to
execute them. Or, simply have an animated gif that you display before
executing long_running_fu nction that says it's busy.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 23 '05 #3
VA wrote:
Suppose I have

setTimeout(foo( ),n);
long_running_fu nction();

Would foo() be executed even when long_running_fu nction() is still
executing? In other words, does the setTimeout() pre-empt anything
else?

[The reason I ask is that I would like to show some feedback to the
user if my long running function takes more than n milliseconds]

Thanks


I'm agree with others writers at this topic.

May be my advice help you. 2 iframes on page like a 2 threads in
process. You can run long function at iframe and use setTimeout() on
the top page.

Sorry for my english..

Oct 23 '05 #4
Random wrote:
VA wrote:
Suppose I have
[1] setTimeout(foo( ),n);
long_running_fu nction();

Would foo() be executed even when long_running_fu nction() is still
executing? In other words, does the setTimeout() pre-empt anything
else?


Nope. JavaScript is single-threaded, so the return value of foo() won't
be executed until after long_running_fu nction finishes.


Rubbish. Have you even bothered to try? JS evaluation is single-threaded,
but that does not mean that only one line of code can be evaluated at the
same time. It means instead that lines of code can only be evaluated at
approximately the same time if they are in different contextes. This is
the case here.

Evaluation within long_running_fu nction() (short for later: lrf()) takes
place within its *local* context, while the evaluation of the return value
of foo() takes place in the *global* context. If lrf() is still running
after approximately n milliseconds, the return value of foo() as it has
been *before* the call of lrf() is evaluated. Try

function foo()
{
return "alert(j);" ;
}

var j;

function lrf()
{
for (var i = 0; i < 1000; i++)
{
for (j = 0; j < 1000; j++);
}
}

setTimeout(foo( ), 25);
lrf();

The alert() box said 919 when I tested. Which means that the
script engine was still executing lrf() at the time it appeared.

However, it seems that the OP was looking for

setTimeout(foo, n);

or

setTimeout("foo ()", n);

instead, where foo() would be *called* after approximately n milliseconds
even if lrf() was still running.
PointedEars
Oct 23 '05 #5
Thomas 'PointedEars' Lahn wrote:
JavaScript is single-threaded, so the return value of foo() won't
be executed until after long_running_fu nction finishes.
Rubbish. Have you even bothered to try?


Yes, and I am alternately frustrated and relieved that it doesn't work.

<snip /> function foo()
{
return "alert(j);" ;
}

var j;

function lrf()
{
for (var i = 0; i < 1000; i++)
{
for (j = 0; j < 1000; j++);
}
}

setTimeout(foo( ), 25);
lrf();

The alert() box said 919 when I tested. Which means that the
script engine was still executing lrf() at the time it appeared.
Gives me 1000 every time.
Except when it set it to go for 10000 i's and 10000 j's. Then it gave
me 10000. (And it took a while and a lot of clicks to tell it to keep
going, believe me.)

Both FireFox and IE on Win2000.

For obvious reasons, I only did one run of 10000 i's & 10000 j's, on
each browser, but the results were the same.

However, it seems that the OP was looking for

setTimeout(foo, n);

or

setTimeout("foo ()", n);

instead, where foo() would be *called* after approximately n milliseconds
even if lrf() was still running.


Quite probably.
--
-r

Oct 23 '05 #6
Random wrote:
Thomas 'PointedEars' Lahn wrote:
function foo()
{
return "alert(j);" ;
}

var j;

function lrf()
{
for (var i = 0; i < 1000; i++)
{
for (j = 0; j < 1000; j++);
}
}

setTimeout(foo( ), 25);
lrf();

The alert() box said 919 when I tested. Which means that the
script engine was still executing lrf() at the time it appeared.
Gives me 1000 every time.
Except when it set it to go for 10000 i's and 10000 j's. Then it gave
me 10000.


Of course it depends on the speed of the machine it is running on.
(And it took a while and a lot of clicks to tell it to keep going,
believe me.)
Which also could have influence on the test result.
Both FireFox and IE on Win2000.
I tested with Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12)
Gecko/20050922 Firefox/1.0.7 (Debian package 1.0.7-1) Mnenhy/0.7.2.0
on Pentium III (Coppermine), ca. 871 MHz clock rate, 256 KiB cache.

With the original code I get the warning, too, but the value the alert
shows is below 1000 every time (out of 10).
For obvious reasons, I only did one run of 10000 i's & 10000 j's, on
each browser, but the results were the same.


Seems a more reliable test is needed.
PointedEars
Oct 23 '05 #7
Thomas 'PointedEars' Lahn wrote:
Random wrote:
Thomas 'PointedEars' Lahn wrote:
function foo()
{
return "alert(j);" ;
}

var j;

function lrf()
{
for (var i = 0; i < 1000; i++)
{
for (j = 0; j < 1000; j++);
}
}

setTimeout(foo( ), 25);
lrf();

The alert() box said 919 when I tested. Which means that the
script engine was still executing lrf() at the time it appeared.


Gives me 1000 every time.
Except when it set it to go for 10000 i's and 10000 j's. Then it gave
me 10000.


Of course it depends on the speed of the machine it is running on.


Which was the main reason I tried the 10000 iterations, actually. Just
to be sure.
(And it took a while and a lot of clicks to tell it to keep going,
believe me.)


Which also could have influence on the test result.


Indeed, but I didn't get the message 'a script on this page is causing
[Firefox/Internet Explorer] to run slowly' on the 1000 iteration.
Both FireFox and IE on Win2000.


I tested with Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12)
Gecko/20050922 Firefox/1.0.7 (Debian package 1.0.7-1) Mnenhy/0.7.2.0
on Pentium III (Coppermine), ca. 871 MHz clock rate, 256 KiB cache.

With the original code I get the warning, too, but the value the alert
shows is below 1000 every time (out of 10).


Don't mean to be insulting, but have to be detailed: you didn't just
hit enter/spacebar on the alert, right? The default selection (in both
IE & FF on Win2k) is to stop the script. (OK/Cancel in FF-- have to hit
cancel for it to keep running; Yes/No in IE-- have to hit no. I made
that mistake once and it came back with something like 4500, which
perturbed me somewhat until I realised what I'd done.)
For obvious reasons, I only did one run of 10000 i's & 10000 j's, on
each browser, but the results were the same.


Seems a more reliable test is needed.


If you can think of one. I'm not sure what else could be done, beyond
10000*10000 iterations, waiting for an alert. I would think the 'error'
message would give it a perfect opportunity to yield to another opcode
(to borrow a concept from another language) if that's what it were
going to do. Even so, 25 milliseconds had definitely come and long-gone
under both 1000^2 and 10000^2.

Like I said, if you can think of a more reliable test (given available
hardware), I'm game.

--
-r

Oct 23 '05 #8

Thomas 'PointedEars' Lahn wrote:
I tested with Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12)
Gecko/20050922 Firefox/1.0.7 (Debian package 1.0.7-1) Mnenhy/0.7.2.0
on Pentium III (Coppermine), ca. 871 MHz clock rate, 256 KiB cache.

With the original code I get the warning, too, but the value the alert
shows is below 1000 every time (out of 10).

I got 1000 every time on XP with IE 6.0 and Firefox 1.06. I and my
colleagues have done extensive testing on the Windows platform and IE
because our business depends on interaction between ActiveX controls
and Javascript. Javascript is indeed single threaded.

Obviously there are parts of the browser's rendering engine that are
not single threaded, image loading and rendering, object loading. It
disturbs me that Mozilla's implementation on Linux is somehow causing
javascript evaluation to get pre-empted. If that's true then what you
are saying about contexts makes sense.

However, I can say that I have never seen it happen on the Window's
platform and like Random, I'm both relieved and frustrated.

beegee

Oct 24 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14952
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
2
4672
by: Athanasius | last post by:
Could someone shed some light as to why the following setTimeout function will not work on the Mac IE5.2? It does however work on PC(Forefox,Netscape,IE) & Mac(Safari,Firefox). Here is the script, it should simply present an alert message after 5 seconds. Thanks. <html> <head> <script language="javascript"> function MsgTimer() { var self = this;
6
2253
by: Brent | last post by:
Is there any obvious reason why there's no delay in the execution of setting the id.style.display when this function gets called? function Hide(divId,timeout) { var id = document.getElementById(divId); setTimeout(id.style.display = 'none',timeout); } Thanks for any help.
12
5556
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
4
5235
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a javascript function which calls setTimeout and will process a second javascript function "Warn" just before the session expires. The Warn function displays an html page with a button. A second timer is started to cause the html page to close...
15
3796
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first image rotates. I cannot figure out how to accomplish the 3 second delay. My code is pasted below: function randPic(){ randPic1(); randPic2();
8
2051
by: Richard Maher | last post by:
Hi, I am in a mouseup event for button A and I'd like to disable=false button B before starting some work. Is there anyway that an event for button B can then fire before my event processing for button A's mouseup has completed? I beleive event processing to be single-threaded for good reason but I need a "stop" button and it's no good if it doesn't do anything until the other processing has finished :-)
4
1524
by: Rex the Strange | last post by:
Seriously. I have a flash animation that I was trying to run during ajax calls - sort of eyecandy - but it would freeze whenever the ajax call was being made. Then I found out it wasn't just the flash, it was the entire page - could scroll, type, do jack. Asynchronous javascript? Yeah, right. (and before you suggest it, that isAscync parameter in the open function doesn't seem to do jack unless I'm not using a boolean correctly)
5
3214
by: Roadworrier | last post by:
Hey, does anyone know how I can pause the processing of XMLHttpRequests so that I can have the foreground interface respond to user clicks? I have 40 graphs drawing to the screen after getting 1000 points each from a cgi contact with a remote server. (40 XMLHttpRequest calls, and 40,000 data points on which some statistics are calculated.) If the user clicks on a button, it can take as long as 10 seconds for the javascript that fires...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.