Cum pot sa fac un delay in PHP...

Secţiune dedicată începatorilor.

Moderatori: Moderatori, Start Moderator

daniel_bondoc
PHPRomania Supporter
Mesaje: 16
Membru din: Mie Oct 12, 2005 6:38 pm

Cum pot sa fac un delay in PHP...

Mesajde daniel_bondoc » Sâm Oct 07, 2006 10:01 pm

Am urmatorul script:
<?php
echo 'text 1';
echo 'text 2';
?>

As vrea ca sa pot intarzia text 2 astfel incat sa apara la un anumit interval de timp fata de text 1 de exemplu la 3 secunde. Am incercat sa folosesc functia sleep () inserand-o intre cele 2 echo dar imi intarzie executarea intregului script. Va multumesc anticipat daca ma puteti ajuta!



Avatar utilizator
Dodo
Senior Member
Mesaje: 958
Membru din: Mar Aug 22, 2006 2:40 pm
Localitate: Constanta
Contact:

Mesajde Dodo » Sâm Oct 07, 2006 10:04 pm

Pai intai dai flush si dupaia sleep :)

Avatar utilizator
punctweb
Senior Member
Mesaje: 509
Membru din: Mie Mar 24, 2004 3:32 pm
Contact:

Mesajde punctweb » Sâm Oct 07, 2006 11:30 pm

PHP: sleep - Manual :: User notes scrie:it is a bad idea to use sleep() for delayed output effects as

1) you have to flush() output before you sleep

2) depending on your setup flush() will not work all the way to the browser as the web server might apply buffering of its own or the browser might not render output it thinks not to be complete

netscape for example will only display complete lines and will not show table parts until the </table> tag arrived

so use sleep if you have to wait for events and don't want to burn to much cycles, but don't use it for silly delayed output effects!

daniel_bondoc
PHPRomania Supporter
Mesaje: 16
Membru din: Mie Oct 12, 2005 6:38 pm

Mesajde daniel_bondoc » Dum Oct 08, 2006 10:44 pm

A incercat sa folosesc flush() dar nu prea mi-a iesit. Poate ma ajutati pe exemplul meu de script sa imi completati codul...Multumesc!

johnutz
Senior Member
Mesaje: 956
Membru din: Mar Iul 20, 2004 9:23 am
Localitate: Între scaun şi tastatură

Mesajde johnutz » Lun Oct 09, 2006 12:49 pm

mai e ceva:
http://ro.php.net/manual/en/function.flush.php scrie: Even the browser may buffer its input before displaying it. Netscape, for example, buffers text until it receives an end-of-line or the beginning of a tag, and it won't render tables until the </table> tag of the outermost table is seen.

Some versions of Microsoft Internet Explorer will only start to display the page after they have received 256 bytes of output, so you may need to send extra whitespace before flushing to get those browsers to display the page.


Mai demult am facut cam asa: [php]<?php
echo 'text1';
echo '<!-- ' . str_repeat(' ', 256) . '-->';
sleep(3); // de fapt, in loc de sleep apelam un executabil cu system(), deci se muncea, nu se dormea ;)
echo 'text2';
echo '<!-- ' . str_repeat(' ', 256) . '-->';
?>[/php]
asta e de cand eram tanar :P

demonu
Average Member
Mesaje: 76
Membru din: Mie Apr 12, 2006 8:16 pm
Contact:

Mesajde demonu » Mar Oct 10, 2006 12:52 pm

Sar putea sa nu fie exact ceea ce cauti, dar javascript te-ar ajuta mult mai mult.

Daca combini window.setInterval() si window.setTimeout() cu document.write() efectul vazut de vizitator va fi acela de text scris cu intarziere, cu toate ca pagina a fost trimisa toata odata fara sleep.

daniel_bondoc
PHPRomania Supporter
Mesaje: 16
Membru din: Mie Oct 12, 2005 6:38 pm

Mesajde daniel_bondoc » Dum Oct 15, 2006 3:25 pm

Am reusit sa fac ceva cu ajutorul JavaScript. Problema este ca se comporta diferit in functie de browser (timpii de delay difera cu cateva secunde...). Oricum e ok. Va multumesc mult tuturor!


Înapoi la “PHP Incepători”

Cine este conectat

Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 27 vizitatori