SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I do I use a PHP function like time() to do something every 20 seconds?

    In python, I would simply have a loop with:

    time.sleep(25)
    do_something()

    How do I do this in PHP?

  2. #2
    SitePoint Enthusiast jwh335's Avatar
    Join Date
    Dec 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    while(1 == 1)
    {
    sleep(20);
    do_something();
    }

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,192
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    In PHP the "time" is "understood" so you can use sleep() where the argument is in seconds. http://us3.php.net/sleep Or use usleep() with microseconds.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great! I think I'll use while(True), if that will work.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you plan to run this through a webserver, you may have timeout issues.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    @pythonista

    Off Topic:


    Seeing as you seem to be a Python user, anything to contribute on this thread from todays discussions?
    We'd be obliged for any input/views.

    Thanks


  7. #7
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, most servers have a time limit on max execution time for running php. You should better try to use cron. And if your hosting doesn't support that then, don't worry! I have heard of external sites that offer cron services.
    Free automatic directory submission
    Relieve stress and read more about stress relief.

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I checked, and my server actually doesn't set any time limit.

    Anyway, using sleep() does not work. When I used sleep() the webpage didn't load for, well, I stopped after waiting 10 seconds.

    I'm planning on doing this:

    if (time() % 25 == 0)
    { etc.

    Is this an inefficient or ugly way of doing things? (Just a reminder, the point is to get the variable $font to change every 25 seconds.)

    Also, is there any way to use PHP so that one does not have to refresh the page to see things change?
    Last edited by Pythonista; May 7, 2009 at 09:05.

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, the above is not working, even with refreshing the page.

  10. #10
    SitePoint Enthusiast jwh335's Avatar
    Join Date
    Dec 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would think it odd if your server didn't set a limit.

    Is it still in an infinite loop? If so, I believe that might be your problem.

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is not an infinite loop that way. I am sure of that.

    The if statement is run everytime someone loads the page. Maybe they do set a limit. I have hostgator, and the person in the online chat said that there was no limit to the amount of time. Perhaps he made a mistake, or misunderstood the question. But, anyway, I think that it is irrelevant, because I don't plan on having a infinite loop since I've discovered that this means that the page will load the HTML only up until the loop, and then wait until the loop outputs HTML.

  12. #12
    SitePoint Enthusiast jwh335's Avatar
    Join Date
    Dec 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, ok. I assumed you had left it in the while loop.

    I'm not sure why the above won't work, then :/

    Edit: I'm going to second the suggestion to look into cron. It's built for stuff like this.

  13. #13
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,192
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    Are you wanting to change the page after it has been rendered?
    Quote Originally Posted by Pythonista
    the point is to get the variable $font to change every 25 seconds
    sleep() runs on the server not the client. I've used it to put delays into mail() and lengthy filesystem tasks. But if you want to change font on the client, javascript would be a better choice.

  14. #14
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've decided to abandon this cool but complicated way to try out different fonts on my webpage. I was also planning to do other things, like writing a program to go through different combinations, giving me 20 seconds to look at each one, and automatically refreshing the page.

    I've decided to just try out fonts "manually," by changing the CSS.


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
  •