PHP Loop; How to Print Each Result and Delay It for a Second Before Echoing Another Result

PHP loop; how to print each result and delay it for a second before echoing another result?

Servers usually buffer the output of a server side script until there's enough in it to output
try something like this. Combination of setting output buffering off and manually flushing the buffer. Note the implcit flush line and the flush and ob_flush lines.

<?php 
@ini_set("output_buffering", "Off");
@ini_set('implicit_flush', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('max_execution_time',1200);

header( 'Content-type: text/html; charset=utf-8' );

echo "Testing time out in seconds\n";
for ($i = 0; $i < 1150; $i++) {
echo $i." -- ";

if(sleep(1)!=0)
{
echo "sleep failed script terminating";
break;
}
flush();
ob_flush();
}

?>

one second intervals in loop result

Well, the problem with setTimeOut() is that when the first oneNumber() function is executed, the loop has finished, and y=31. I think that there will be a way to fix a variable value when you call setTimeOut() function, but I've not found it.
That's the only way I found out:

<!DOCTYPE html>
<body>
<?php
for ($i=1; $i<=30; $i++) {
$roll = mt_rand(1,3);
if ($roll==1) {echo '<b id="b'.$i.'" style="display: none;"> 1</b>';}
elseif ($roll==2) {echo '<b id="b'.$i.'" style="display: none;"> 2</b>';}
else {echo '<b id="b'.$i.'" style="display: none;"> 3</b>';}
}
?>
<script>
window.onload = function () {

window.setTimeout(function() {
document.getElementById('b1').style.display="block";
},1000)
window.setTimeout(function() {
document.getElementById('b2').style.display="block";
},2000)
window.setTimeout(function() {
document.getElementById('b3').style.display="block";
},3000)
window.setTimeout(function() {
document.getElementById('b4').style.display="block";
},4000)
window.setTimeout(function() {
document.getElementById('b5').style.display="block";
},5000)
window.setTimeout(function() {
document.getElementById('b6').style.display="block";
},6000)
window.setTimeout(function() {
document.getElementById('b7').style.display="block";
},7000)
window.setTimeout(function() {
document.getElementById('b8').style.display="block";
},8000)
window.setTimeout(function() {
document.getElementById('b9').style.display="block";
},9000)
window.setTimeout(function() {
document.getElementById('b10').style.display="block";
},10000)
window.setTimeout(function() {
document.getElementById('b11').style.display="block";
},11000)
window.setTimeout(function() {
document.getElementById('b12').style.display="block";
},12000)
window.setTimeout(function() {
document.getElementById('b13').style.display="block";
},13000)
window.setTimeout(function() {
document.getElementById('b14').style.display="block";
},14000)
window.setTimeout(function() {
document.getElementById('b15').style.display="block";
},15000)
window.setTimeout(function() {
document.getElementById('b16').style.display="block";
},16000)
window.setTimeout(function() {
document.getElementById('b17').style.display="block";
},17000)
window.setTimeout(function() {
document.getElementById('b18').style.display="block";
},18000)
window.setTimeout(function() {
document.getElementById('b19').style.display="block";
},19000)
window.setTimeout(function() {
document.getElementById('b20').style.display="block";
},20000)
window.setTimeout(function() {
document.getElementById('b21').style.display="block";
},21000)
window.setTimeout(function() {
document.getElementById('b22').style.display="block";
},22000)
window.setTimeout(function() {
document.getElementById('b23').style.display="block";
},23000)
window.setTimeout(function() {
document.getElementById('b24').style.display="block";
},24000)
window.setTimeout(function() {
document.getElementById('b25').style.display="block";
},25000)
window.setTimeout(function() {
document.getElementById('b26').style.display="block";
},26000)
window.setTimeout(function() {
document.getElementById('b27').style.display="block";
},27000)
window.setTimeout(function() {
document.getElementById('b28').style.display="block";
},28000)
window.setTimeout(function() {
document.getElementById('b29').style.display="block";
},29000)
window.setTimeout(function() {
document.getElementById('b30').style.display="block";
},30000)
}

</script>
</body>
</html>

Javascript to display PHP value with one second delay

Yes, you'll need JavaScript for this. All PHP can do is emit the values to client-side code. Then it's up to the client-side code to display those values.

So, for example, your PHP code can populate a JavaScript array:

<script type="text/javaScript">
var values = <?php echo json_encode($php_variable); ?>;
</script>

In this case $php_variable would be an array containing the values you want to display. Now your client-side code has an array called values which it can iterate. You can use setTimeout to schedule something to happen with a delay. Iterating over that array, it might look something like this:

var index = 0;
var displayValue = function () {
if (index >= values.length) { return; }

var value = values[index++];
// display "value" somewhere on your page

setTimeout(displayValue, 1000);
};
displayValue();

This should display each value in the array with 1-second increments. Note that I sort of glazed over the "display somewhere on your page" part, since that's up to you really. Where/how are you looking to display this? This works a little differently client-side than it does server-side, since the entire HTML document is already rendered. You need to identify where in that document you want to display the value and display it there, not just "echo" it.

Add delay before PHP echo

You could use PHP sleep to wait 30 seconds:

<body>
<p>the string will appear in 30 seconds:</p>
<p><?php sleep(30); echo $string; ?></p>
</body>

However, when you do this, depending on your PHP and Apache settings, your entire page may wait 30 seconds before fully rendering and sending an HTTP response back to the client.

Additionally, if you send a partial response to the client, it may sometimes wait to render content until enough of the page is loaded to present the data (e.g. in the case of a table).

Try using JS instead, to allow the page to render and then present the hidden data after 30 seconds:

<body>
<p>the string will appear in 30 seconds:</p>
<p id="sleep" style="display:none"><?php echo $string; ?></p>
<script>
window.setTimeout(function() {
document.getElementById('sleep').style.display = '';
}, 30000);
</script>
</body>

PHP sleep() not working how i'd hoped in redirect

Send out your error message nromally and use a meta refresh to do the redirecting after x amount of seconds:

<?php

// your code that determines there is an error goes here

header('Refresh: 10;url=your_page.php');

echo $errorMessage;

The header() call is identical to this meta tag:

<meta http-equiv="refresh" content="10;URL='your_page.php'">

How to optimize this array loop

To answer your questions:

but it take more time..how to reduce this big time?

Before you can reduce that you need to find out exactly where that more comes from. As you wrote in a comment, you are using a remote request to obtain the data.

Sorting for array the data you've provided works extremely fast, so I would assume you don't need to optimize the array sorting but just the way when and how you get the data from remote. One way to do so is to cache that or to prefetch it or to do parallel processing. But the details so far are not important at all, unless you've found out where that more comes from so that it is clear what is responsible for big time so then it can be looked into reducing it.

Hope this is helpful so far, and feel free to add the missing information to your question.

You can see the array-only code in action here, it's really fast:

  • https://eval.in/51770

You can find the execution stats just below the output, exemplary from there:

OK (0.008 sec real, 0.006 sec wall, 14 MB, 99 syscalls)

Build a countdown using a loop and sleep method in php

You may achieve it by flushing the output before the sleep

The code will be :

<?php

$myarr=array(1,2,3,4,5,6,7,8,9,10);
foreach($myarr as $p){
//print out a number and then pause for two seconds

ob_start();

echo "$p <br/>";

$size = ob_get_length();
ob_end_flush();
@ob_flush();
flush();

sleep(2);

}
?>


Related Topics



Leave a reply



Submit