/    Sign up×
Community /Pin to ProfileBookmark

sleep() Function Mystery

Folks,

This is just ridiculous.
Let’s say you want your screen to echo ‘1’.
Then 2-5 secs later, echo ‘2’.
How’d you write the code ? Like this ?

[code]
<?php

echo ‘1’;
sleep(rand(2,5));
echo ‘2’;

die;
?>
[/code]

Guess what ?
When I load the page, browser echoes no ‘1’ and waits 2-5 secs and then echoes simultaneously: 12.
Why ?

to post a comment

11 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 11.2021 — Guess what, the PHP code runs on the web server, not in the browser. You should use JavaScript if you want something like that to happen in the browser.
Copy linkTweet thisAlerts:
@VITSUSAApr 12.2021 — @developer_web#1630220 I agree with NogDog, you will have to use JavaScript to run the code in the browser because PHP code runs on the webserver.
Copy linkTweet thisAlerts:
@developer_webauthorApr 14.2021 — @NogDog#1630225

In that case, php should not have created a delay function if it's not gonna work properly. They should have programmed the function so two requests are made to the server, in this case. And not one request. All this is happening due to one request being made. In one serving they interprete the whole code. Should've done it in two goes, just like the browser (Javascript) does it.

Silly billies (php vendors)! Lol!
Copy linkTweet thisAlerts:
@NogDogApr 14.2021 — The sleep() is working exactly the way it was designed; but you don't see it in the client because of the way the HTTP interaction is buffering data. It doesn't send each and every byte one at a time, but in pre-determined sizes of "clumps" of bytes. So your PHP outputs a byte or two, it then sleeps for a couple seconds, then it outputs the next byte. If that first output didn't happen to be enough to flush the HTTP output buffer, then that means all the bytes get sent together once it does. In your example, from a practical standpoint, all the sleep is doing is delaying by 2 seconds when the web server sends the output to the browser.

There are ways that _might_ get around this by using output buffering functions, but even then the web server may still do its own buffering, in which case it might not matter. This might work, but it might not:
[code=php]
<?php
echo "<p>1</p>";
ob_flush(); // flush whatever is in the buffer so far
sleep(2);
echo "<p>2</p>";
[/code]
Copy linkTweet thisAlerts:
@tracknutApr 14.2021 — And just to confirm how it's working, a non-believer could run a test like this:
<i>
</i>&lt;?php
printf ("&lt;p&gt;Start time: %s&lt;br&gt;",date('H:i:s', time()));
sleep(2);
printf ("End time: %s&lt;/p&gt;",date('H:i:s', time()));
?&gt;
Copy linkTweet thisAlerts:
@jijoApr 15.2021 — Sleep function is necessary, it has a major role in computers. Personally I always put my laptop to sleep.

For website making <a href="https://www.webdesignlogix.in/">web design company in Trivandrum></a>
Copy linkTweet thisAlerts:
@developer_webauthorApr 15.2021 — @NogDog#1630332

Nope. I'm afraid result of your code, looks the same as the previous code (tutorial's code) on my screen.
Copy linkTweet thisAlerts:
@developer_webauthorApr 15.2021 — @tracknut#1630338

It's not about believing. It's about how my website visitor will experience alerts on their screens.

I just wanted to give them alert messages from my webpage at different intervals.

Like one line per every 10 secs.

But the 3 codes so far on this thread, all seem to come at the screen all at once. A visitor can't read that fast.

So to a user, it would seem all 10 lines appeared on screen at once.

So from a technical point of view, the sleep() maybe working. But from the user or visitor's point of view, it isn't.
Copy linkTweet thisAlerts:
@developer_webauthorApr 15.2021 — @VITSUSA#1630250

Ok.

So do you know Javascript ? Howabout showing a very minimum bit of code to achieve my purpose ?

Let's see how messy it looks compared to php.
Copy linkTweet thisAlerts:
@NogDogApr 15.2021 — > @developer_web#1630372 looks the same as the previous code (tutorial's code) on my screen

As I warned that it might -- another reason not to try to achieve that effect on the server side.
Copy linkTweet thisAlerts:
@inktApr 15.2021 — Learn js, its a huge tool when working with the client side. I use jQuery's delay(###), or js setTimeout if I am in need of a pause.
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...