Does PHP have a function like setTimeout()?

Discussion in 'PHP' started by webshore88, Jun 26, 2012.

  1. #1
    Hi DP Members,
    I have been working on a project and need to create a clock in php, so, does PHP have a function like setTimeout() or something?
     
    webshore88, Jun 26, 2012 IP
  2. shubhamjain

    shubhamjain Active Member

    Messages:
    215
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #2
    Yes, delay() does that for you.
     
    shubhamjain, Jun 26, 2012 IP
  3. akhileshbc

    akhileshbc Active Member

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    5
    Trophy Points:
    75
    #3
    Hi, I couldn't find out that function in PHP's manual. You are talking about a custom function ?

    @webshore88 : Please provide more details regarding what you are trying to do or what functionality that you are needed.
     
    akhileshbc, Jun 27, 2012 IP
  4. Simple Boot

    Simple Boot Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    while (true)
    {
         // Execute
         someFunction();
    
         // Wait before continuing
         sleep(5);
    }
    
    PHP:
     
    Simple Boot, Jun 27, 2012 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    sleep()

    set_time_limit()

    Not sure exactly what you are looking to do.
     
    jestep, Jun 27, 2012 IP
  6. shubhamjain

    shubhamjain Active Member

    Messages:
    215
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #6
    Tch.. Always the confusion between sleep, and delay. My mistake, its sleep().
     
    shubhamjain, Jun 27, 2012 IP
  7. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #7
    Keep in mind the sleep function performs WHILE a connection is live. Once content is delivered the general rule is to disconnect. By forcing the PHP script to sleep you may open yourself up for all kinds of unexpected problems.

    What exactly are you trying to do?
     
    NetStar, Jun 27, 2012 IP
  8. DevSwift

    DevSwift Peon

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #8
    Agree with NetStar, what are you trying to do? sleep() basically completely halts execution for x amount of time, it doesn't perform like setTimeout() in JS whereas you can set a function to execute later and continue executing functions, etc...
     
    DevSwift, Jun 27, 2012 IP
  9. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #9
    In JavaScript setTimeout() calls a function at a set time. PHP doesn't have this feature. And even if you CAN imitate the function it will NOT behave the same. PHP can't change elements in your browsers DOM nor can it control ANYTHING with the browser aside from outputting data. If you want to call a specific function after a page has been loaded you need to use JavaScript...or more specifically some sort of AJAX/Jquery call to rest, create a subliminal new http request to fetch data from a PHP script.
     
    NetStar, Jun 27, 2012 IP
  10. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #10
    It could be possible with some tricks. There is gearman and threading (for linux only) that I know of.
     
    gapz101, Jun 29, 2012 IP
  11. dylanrodriguez2012

    dylanrodriguez2012 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hello Guys.

    When I started using
    Javascript I had read a few tutorials, but I discovered there were many things they had not covered. There is a lot of information on the web but it helps if you search for a particular feature.

    All your comment for php development are welcome.

    Thanks in advance.
     
    dylanrodriguez2012, Jun 29, 2012 IP
  12. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #12
    Thank all of you guys, sleep() is solving my purpose.
     
    webshore88, Jun 29, 2012 IP
  13. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #13
    No it's not. You stated you wanted to "create a clock in php". PHP is not a markup or presentation language. It's a programming language that merely receives/sends/prints data. The only thing you can do with PHP to create a clock is to print the time to the browser. If you want a LIVE clock that updates via the DOM (or I will say document) you will need to use a dynamic language that is read by the browser. That would be JavaScript.

    Using sleep is a terrible idea. Why? Because it's not going to work the way you want it to. sleep() would literally "pause" the entire program. Think of it this way: you're driving down the road to your girlfriends house. Every 15 seconds you must stop for a 30 second red light (sleep(30)). While you are stopped at the light you aren't doing ANYTHING. Then you start moving again but guess what? Your girlfriend will never see you because you are on the wrong road. Using PHP to create a clock would be using the wrong road. You will eventually find out for yourself...
     
    NetStar, Jun 29, 2012 IP