ModS
ModS

Reputation: 846

How to pause for-each iterations php beginner

For some reason this code does not pause in between iterations, i was expecting it to pause for a second between loading thumbnails. what am i doing wrong?

foreach ($media->data as $data) {
  echo "<img src=\"{$data->images->thumbnail->url}\">";
  sleep(1);
}

Upvotes: 0

Views: 2758

Answers (4)

Class
Class

Reputation: 3160

The sleep occurs only on server-side (before output to clients computer). You'll need to do this either do this via js or css3 selectors

your best bet would be to use css3 selector instead of js but you can use either one.

-webkit-transition: opacity 1s;
-moz-transition: opacity 1s;
transition: opacity 1s;

Upvotes: 1

Havenard
Havenard

Reputation: 27864

If you are expecting the generated code to reach the client-side before terminating the script, you may have to force flush it:

foreach ($media->data as $data) {
    echo "<img src=\"{$data->images->thumbnail->url}\">";
    @ ob_flush();
    @ flush();
    sleep(1);
}

But this is very stupid, and will most likely cause formatting issues in client-side. Why don't you just do like everyone else and send the whole page at once?

Or for a better client-side control of what is loaded and in what pace, use JavaScript.

Upvotes: 2

Robbert
Robbert

Reputation: 6582

This delay would only happen on the server. The page and image files would still need to download to the client and be rendered there. If you want something that actually loads images with delays in between, you may want to look into a JavaScript method of loading images.

Upvotes: 1

Jonah
Jonah

Reputation: 16202

The code included is server side code. It runs on the server, produces html, and then sends it to the client. So most likely the only effect you will see is that your page will load more slowly.

In order to get the effect you want, you'll need to use javascript, which runs in the user's browser, and can dynamically generate content with pauses and animations and other effects.

Upvotes: 1

Related Questions