Javascript While Loop
2002-09-14 18:39:45
Category: javascript:general
Description: Example on how to use the while statement in a javascript.
Author: mind
Viewed: 23164
Rating: (34 votes)


<!-- while.js - mind (mind@metalshell.com) -->
 
<!-- this shows a basic example of how to use -->
<!-- the while statement in javascripting -->
<!-- http://www.metalshell.com -->
 
<!-- put this code into the <head> section of your html document. -->
 
<!-- start our javascript -->
<script language=JavaScript>
        // define i as a integer of 0
        var i = 0;
 
        // define max as a integer of 10
        var max = 10;
 
        // define message a string of: This is..
        var message = "This is our message";
 
        // create our function
        function printText()
        {
                // while (condition) { statements; }
 
                // start our while loop
                while (i < max)
                {
                        // increase i with each loop
                        i++;
 
                        // print our message
                        document.write('<font>' + message + '</font><br>');
                }
        }
 
        // never ending while loop: while (1) { statements; }
 
        // end of our javascript
</script>
 
<!-- place this code anywhere on your html document (example) -->
 
<!-- align our link below -->
<div align=center>
        <!-- make a link that points to our js function printText() -->
        <a href="javascript: printText();">Click Here!</a>
</div>