SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pause some of the script?

    I'm trying to figure out how to pause a function for 2 sec but the rest of the page should run and finish. Is this possible?

    I have tried this but with no luck.
    PHP Code:
    echo 'Run this first';

    function 
    xml(){
        
    sleep(10);
        echo 
    'I am delayed...';
    }

    xml();

    echo 
    'This should come before the function?!'
    Thanks in advance...

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP is not javascript. The only way, I think, would be to use forking. What's the purpose behind wanting to do this? There may be a better way.
    - Robert

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by QMonkey View Post
    PHP is not javascript. The only way, I think, would be to use forking. What's the purpose behind wanting to do this? There may be a better way.
    I'm using a script to store some data in my sql database and then creating xml documents from that... I need the script to tell/pretend another script it is finished when the first part is over and then run the xml thing...

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    That is not how PHP works.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    That is not how PHP works.
    What should I do then? javascript?, jquery? Any ideas?

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    What you should do...is not be dependent on such a feature. Why do you need the script to skip that part?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jmansa View Post
    I'm using a script to store some data in my sql database and then creating xml documents from that... I need the script to tell/pretend another script it is finished when the first part is over and then run the xml thing...
    Sounds pretty straight forward to me
    PHP Code:
    doStuff();

    storeData();
    makeXml();

    doMoreStuff(); 
    Maybe you're looking to have a cron job make any new xml docs later on. That would just be a separate thing. Store the data and be done. Then a second script will run periodically - finding any data it hasn't processed yet and making the xml.
    - Robert

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    as QMonkey said.

    You're not wanting a 2 second pause, you're wanting normal program execution. do A, then B when you're done with A, then C when you're done with B.

    PHP does not do asynchronous execution unless you're using fork/exec (IE: Something outside of the script)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  9. #9
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Does the xml function need to output anything?

    You could send the request to create the XML using a JavaScript <script> element. The request for external javascript still gets called even when javascript is turned off in the browser. (In the browsers I tested it does)

    Just calling an external javascript file will execute that file. So you could call a server-side php script from a client-side script element, and as long as it outputs javascript it will execute without creating an error. Even if Javascript is turned off it will still execute the file.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  10. #10
    SitePoint Zealot
    Join Date
    Jun 2010
    Location
    Arizona
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The phrase you are looking for is "asynchronous execution". Parts of PHP support async operations, most of it doesn't, and the parts that do won't function exactly the same on every host.

    It is in your user's best interest to not throw arbitrary delays into code. PHP also has an execution time limit when running on a web host. When PHP runs from the command-line (e.g. via a cron job), there generally is no time limit. This seems like one of those things best left to a long-running cron script to process. Write a temporary file that indicates that the cron script needs to check the database. The cron script checks for that file's existence every second (PHP has some nuisance issues here - see clearstatcache()) - if so, it runs the SQL query looking for what needs processing, does the processing, and then updates the database to indicate that it is done. Of course, you said there is an XML file that is generated, so you can just check to see if it exists at that point.

    The OS is the provider of asynchronous execution in this case instead of trying to do it all from within PHP. Plus, you can simply return to the caller and then use Javascript/jQuery to show a "please wait..." box to the user that pings the server every couple of seconds to see if the process is complete instead of having a web browser hang forever and not fire onready/onload handlers.
    Thomas Hruska

    Single Sign-On Server/Client - The PHP login system that rocks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •