www.webdeveloper.com
Results 1 to 4 of 4

Thread: very simple question about function in javascript

  1. #1
    Join Date
    Jun 2009
    Posts
    4

    very simple question about function in javascript

    This code works like charm on my browser as it should work
    Code:
     <html>
     <script language=javascript>
     function change()
     {
     var da=new Date();
     document.getElementById("date").innerHTML=da; 
     setTimeout("change()",1000);
     } 
      </script>
     <body onLoad = "change()">
     <div id="date"></div>
     </body>
     </html>

    but when i change the code to this no change in time and date
    Code:
     <html>
     <script language=javascript>
     function change()
     {
     var da=new Date();
     document.getElementById("date").innerHTML=da; 
     change();
     } 
      </script>
     <body onLoad = "change()">
     <div id="date"></div>
     </body>
     </html>
    as per my c lanuage experience the function change keeps calling itself creating a sort of infinite loop and keep updating date but it is not happening;

  2. #2
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    JavaScript works too closely with the browser. The changes are happening but so quickly that the browser is waiting for everything to settle down so it can display the final results. You have to give it a delay.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  3. #3
    Join Date
    Jun 2009
    Posts
    4
    OK That is why it hangs when it encounters an infinite loop!!
    thnx for such a fast reply

  4. #4
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    It's not really hanging up, the script is still running. The browser is just waiting for the script to get to the point.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles