0

http://ejohn.org/blog/how-javascript-timers-work/

Javascript has timers for functions, is there something similar in PHP as well. In my script, I want one function to run every 50 seconds, and then continuing the script as it is. Is this possible ?

More information : At the start of the PHP script, via an API I generate a session ID which expires every 60 seconds. We can renew that in 60 seconds. So, what I want is, every 50 seconds, a function runs which will renew that session ID with a curl request and then again continue the PHP script, so that I can retrieve images from the web-server I want. Also, I want to run it from console(infinite running script) and not as a server-side language.

Yash Sodha
  • 713
  • 3
  • 13
  • In what context, as a web service handler, a console program, or as a daemon? – David Oct 11 '14 at 05:46
  • 1
    You know that PHP doesn't run on the client side, right? Why do you need a 50s timer in your script? – Ja͢ck Oct 11 '14 at 05:52
  • 1
    Would it be possible to save the time of the last pull using microtime(true) and then continuously check how much time has passed by running microtime(true) again until the difference is 5, and then do curl request? – Yavor Oct 11 '14 at 06:15
  • That is a option, but still not a good option, as I have a very long script ! and I would have to check that multiple times .. – Yash Sodha Oct 11 '14 at 06:23

2 Answers2

2

PHP runs on the server level - before the page is sent to the browser. If you want something to run every 50 seconds after the page loads then you have to do it using javascript. You can however invoke a php script every 50 seconds using a javascript timer and an ajax call to the script. Is this what you need to do?

Here is a good way to do it.

Community
  • 1
  • 1
Yavor
  • 671
  • 3
  • 9
  • I edited the question adding more details, see if you can help me. :) I am not looking for a javascript, as I am running this as console and infinite script. – Yash Sodha Oct 11 '14 at 05:59
1

This link might help you:- How to run a php function after certain time without using cron

Community
  • 1
  • 1
Garima
  • 174
  • 6
  • You can clearly see in the question that AJAX doesn't come into question ! Cron jobs won't help me as I can't generate new session ID every time ! I want it to be continuously running in the background.. – Yash Sodha Oct 11 '14 at 06:11
  • The detailed description was added on same time when I posted the answer. So didn't know about that. – Garima Oct 11 '14 at 06:18
  • Yes, you are right, can you edit your answer to help me now ? – Yash Sodha Oct 11 '14 at 06:25
  • This link might help you :-http://stackoverflow.com/questions/10895312/how-to-run-a-php-function-after-certain-time-without-using-cron – Garima Oct 11 '14 at 06:25
  • Hey, I can't un-downvote the answer ! Can you please edit your answer to contain that ^ link – Yash Sodha Oct 14 '14 at 06:41
  • Not fully, but still helped me. :) – Yash Sodha Oct 14 '14 at 08:19