Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6330553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:53:31+00:00 2026-05-24T17:53:31+00:00

Consider this JavaScript Code: <script type=text/javascript> function loop(Message){ document.getElementById(‘output’).innerHTML = document.getElementById(‘output’).innerHTML + Message +

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:53:32+00:00Added an answer on May 24, 2026 at 5:53 pm

    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this Asp.net page code: <head runat=server> <title></title> <script type=text/javascript> function showhide(master, detail) {
Consider this code below: <a href="javascript:void(-1)" id="a1">a1</a> <a href="javascript:void(-1)" id="a2">a2</a> <script type="text/javascript"> var buttons
Consider this code: <script type=text/javascript> if ('mySuperProperty' in window) { alert(window['mySuperProperty']); } var mySuperProperty
Consider the following html snippet <!DOCTYPE html> <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-1.4.2.js ></script> <script
Analyzing the location.hash with this simple javascript code: <script type="text/javascript">alert(location.hash);</script> I have a difficult
Consider the following piece of code: <html><head></head> <body> <script type=text/javascript> var outside_scope = outside
Consider this javascript code: var bar = function () { alert(A); } var foo
Consider this JavaScript code... (function() { window.myNameSpace_callback = function() { // Do whatever. delete
Consider a javascript file script.js which contains the code alert(theVar); Now, i linked the
Consider this example Javascript code: a = new Array(); a['a1']='foo'; a['a2']='bar'; b = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.