Which thread executes javascript code?

M

morgan.chengmo

Hi,

I am wandering whether all javascript code in one page is executed in
one thread. I know that javascript has no threading mechansim. No way
to tell which thread is running by code iteself.

For below code
<html>
<head>
</head>
<body>
<script type="text/javascript">
function onTimeout()
{
alert("time out");
}

setTimeout("onTimeout()", 5000);

while (true)
{
}
</script>
</body>
</html>
If there is only one thread and event loop, I suppose the script will
end in dead loop. But, in IE, the alert pops up after 5 seconds. How to
explain it?

Does each browser page has only one thread interpreting script?

Thanks,
 
R

Richard Cornford

I am wandering whether all javascript code in one page is
executed in one thread.

Javascript is not multithreaded.
I know that javascript has no threading mechansim. No way
to tell which thread is running by code iteself.

Where there are no threads there is no need to know "which thread is
running".
For below code
<html>
<head>
</head>
<body>
<script type="text/javascript">
function onTimeout()
{
alert("time out");
}

setTimeout("onTimeout()", 5000);

while (true)
{
}
</script>
</body>
</html>
If there is only one thread and event loop, I suppose the script will
end in dead loop.

Or when IE puts up the 'a script on this page is causing IE to run
slowly" dialog and offers the user an opportunity to abort it.
But, in IE, the alert pops up after 5 seconds.

No it does not. I suppose that if an IE version was set up to
automatically abort scripts that make it run slowly rather than putting
up the dialog then you may see that.
How to explain it?

Poor observations leading to false deductions.
Does each browser page has only one thread interpreting script?

Javascript is not multithreaded.

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top