Filipe Santos
Filipe Santos

Reputation: 1671

Why doesnt JavaScript block?

Consider this JavaScript Code:

<script type="text/javascript">

function loop(Message){
document.getElementById('output').innerHTML = 
document.getElementById('output').innerHTML + Message + '</br>';
}

window.setInterval("loop('Message1')", 1000); //Prints "Message1" every 1 Seconds
window.setInterval("loop('Message2')", 3000); //Prints "Message2" every 3 Seconds

</script>

<body>
<div id="output"></div>
</body>

Output:
Message1
Message1
Message2
Message1
Message1
Message1
Message2
...

And now Consider this PHP-Code:

<?php
while(true){               // Print "Message1" every 1 Second
echo 'Message 1 </br>';
sleep(1);
}



while(true){              //This Code will never be executed, 
echo 'Message 2 </br>';    //because the First Loop Blocks the Process!!!!!
sleep(3);
}

?>

Output:
Message1
Message1
Message1
Message1
Message1
Message1
...

Why doesn't the first JavaScript-Loop stop the whole Process like the first while-Loop in PHP?

I know that Javascript is SingleThreaded, so I suppose, that JavaScript can't just start a new Thread to process the Second Loop. So I am woundering how JavaScript doesn't block here?

Could someone explain it to me?

Upvotes: 0

Views: 196

Answers (6)

user882255
user882255

Reputation:

Previous answers are alright but I have something to add. Perhaps it will be helpful for you.

Your JavaScript code in PHP will look like this:

<?php
function loop($message, $duration, $multiplier){
    while($multiplier--){
        echo $message;
        sleep($duration);
    }
}
while(true){
    loop("Message1", 1, 2);
    loop("Message2", 1, 1);
}
?>

But you have to remember what is in previous posts (in one PHP script one command at one time).

Upvotes: 1

deceze
deceze

Reputation: 522372

Because setInterval is specifically not sleep.

setInterval is more like a scheduler. It marks a time at which something should be executed and lets the main thread forget about it completely until that time arrives.

sleep on the other hand is basically a no-op loop, which lets nothing else execute while it's looping.

This is important, since the use cases for PHP and Javascript are entirely different. A PHP script has a start and an end and it executes in a linear fashion. Javascript is used for interactivity, which means it needs to respond to various things (mouse clicks, keyboard input, timers, AJAX callbacks) in a non-linear fashion as they happen. If the main thread was blocked waiting for a timer, nothing would happen and the interface would appear frozen.

Here's a good article by jQuery's John Resig about Javascript timers: http://ejohn.org/blog/how-javascript-timers-work/

Upvotes: 6

Pablo Fernandez
Pablo Fernandez

Reputation: 105238

Javascript, being single threaded, can't block. If it did, the whole browser activity (clicking, rendering, etc.) would stop waiting for something to happen.

That's why expensive operations (like ajax requests) use a callback mechanism to handle responses.

Now, with your particular example setInterval works that way by design (like setTimeout). They handle the sleep use case without blocking the main thread.

Upvotes: 0

Andreas K&#246;berle
Andreas K&#246;berle

Reputation: 110972

As JavaScript is single threaded there is only on stack with commands to execute. Every time setInterval is called the function will be put on the stack and will be execute when all previous ones are execute. This very usefull when you have bunch of data to compue but wants the ui to be not blocked for user input. But when you would use JavaScripts while it would have the same blocking effect like in php

Upvotes: 1

Jason Gennaro
Jason Gennaro

Reputation: 34855

You are not using a javascript while loop.

You are using a function with a setInterval.

They are different.

Here's more on a js while loop: http://en.wikipedia.org/wiki/JavaScript_syntax#While_loop

Upvotes: 0

Joachim Sauer
Joachim Sauer

Reputation: 308131

Because you've not written any loops.

You've just registered some code that should be run at a specified interval.

So once every second loop is executed with the argument 'Message1' and once every 3 seconds it is executed with the argument 'Message3'. But since each invocation returns pretty quickly, there won't be any problem with running the other one just after the first one.

Upvotes: 0

Related Questions