Jump to content

While Loop


Recommended Posts

With this script:

 

<?php
$i=1;
while($i<=5) {
echo "The number is " . $i . "<br />";
$i++;
sleep(3);
}
?>

 

I get:

 

The number is 1

The number is 2

The number is 3

The number is 4

The number is 5

 

How can I make this dynamic. For example:

 

First it would get:

 

The number is 1

 

After 3 seconds...

 

The number is 2

 

After 3 seconds...

 

The number is 3

 

.......

Link to comment
https://forums.phpfreaks.com/topic/246704-while-loop/
Share on other sites

I think what err.. adhbvklwqdbviabjiawdnbij is after, is the text appearing dynamically on the page - i.e. JavaScript. It's very simple to do. Run this and let us know if it's what you're after...

 

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

<script type="text/javascript">
    var i = 0;
    var timer = setInterval(function() {
        document.getElementById('output').innerHTML += 'The number is ' + ++i + '<br>';
        if (i == 5) {
            clearInterval(timer);
        }
    }, 3000);
</script>

Link to comment
https://forums.phpfreaks.com/topic/246704-while-loop/#findComment-1266848
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.