worth2talk Posted February 10, 2008 Share Posted February 10, 2008 Hi PHP Guru, I am a newbie and looking for help from you. Okay..lets come to the problem... I wrote a php function which executes some Java scripts as below (for example) : // index.php <body> <?php include("func.php"); display(); ?> </body> --------------------------------------- // func.php <?php function display() { echo' <div class="dispMain"> <script type="text/javascript"> // Some Javascript code ... </script> </div>'; } ?> Well, everything is working fine. What i want is to make sure display() function in index.php is called only when the entire page is loaded... how can i do that ? Is it possible to provide some delay such that display() is called only after that delay ? Fast response will be appreciated ..!! Thanks in Advance. Quote Link to comment https://forums.phpfreaks.com/topic/90335-php-help-required/ Share on other sites More sharing options...
PHP Monkeh Posted February 10, 2008 Share Posted February 10, 2008 Would it not be easier just to call the JS function at the bottom of the page? Quote Link to comment https://forums.phpfreaks.com/topic/90335-php-help-required/#findComment-463180 Share on other sites More sharing options...
worth2talk Posted February 10, 2008 Author Share Posted February 10, 2008 Well, it would be easy. However website took some time to display main content. It basically fetches data from other resource dynamically which may cause some delay. And the problem is when site is opened, javascript inside display() is executed before other content. so i just want to add some delay in function call. Quote Link to comment https://forums.phpfreaks.com/topic/90335-php-help-required/#findComment-463194 Share on other sites More sharing options...
Sulman Posted February 10, 2008 Share Posted February 10, 2008 Have a look at the javacript function setTimeout(); With it you can delay your JS running. Quote Link to comment https://forums.phpfreaks.com/topic/90335-php-help-required/#findComment-463202 Share on other sites More sharing options...
worth2talk Posted February 10, 2008 Author Share Posted February 10, 2008 I dont know about setTimeout function. But my intension is to delay php function execution rather than javascript. Is there any way to delay execution of php function ? same as what setTimeout function does for Java Scripts..?? Quote Link to comment https://forums.phpfreaks.com/topic/90335-php-help-required/#findComment-463210 Share on other sites More sharing options...
harristweed Posted February 10, 2008 Share Posted February 10, 2008 RTFM http://es.php.net/sleep Quote Link to comment https://forums.phpfreaks.com/topic/90335-php-help-required/#findComment-463211 Share on other sites More sharing options...
Sulman Posted February 10, 2008 Share Posted February 10, 2008 this could do it but this will stop ALL execution. Quote Link to comment https://forums.phpfreaks.com/topic/90335-php-help-required/#findComment-463213 Share on other sites More sharing options...
worth2talk Posted February 10, 2008 Author Share Posted February 10, 2008 Sleep() basically delay the execution of all the code written after the sleep(). It doesn't useful when you want to delay the particular function. Let me be more specific towards my problem. Consider the same code snippet: // index.php <body> <?php include("func.php"); ?> <!-- Some HTML -1 code goes here --> <?php display(); ?> <!-- Some HTML - 2 code goes here. This code should not be delayed because of delay in display() --> </body> // func.php <?php function display() { echo' <div class="dispMain"> <script type="text/javascript"> // Some Javascript code ... </script> </div>'; } ?> I want to delay the display() routine, such that the javascript inside display() will execute after few seconds. However "HTML - 2" will not get delayed. So the page display sequence should be as below: 1. HTML - 1 code executes 2. HTML - 2 code executes 3. display() (due to delay executed after HTML-2 code) Quote Link to comment https://forums.phpfreaks.com/topic/90335-php-help-required/#findComment-463215 Share on other sites More sharing options...
janim Posted February 10, 2008 Share Posted February 10, 2008 i think AJAX is best solution here Quote Link to comment https://forums.phpfreaks.com/topic/90335-php-help-required/#findComment-463240 Share on other sites More sharing options...
worth2talk Posted February 10, 2008 Author Share Posted February 10, 2008 how the Ajax will be useful ? Is this a limitation of PHP ? Quote Link to comment https://forums.phpfreaks.com/topic/90335-php-help-required/#findComment-463372 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.