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.

4
  • In what context, as a web service handler, a console program, or as a daemon? Commented Oct 11, 2014 at 5:46
  • 1
    You know that PHP doesn't run on the client side, right? Why do you need a 50s timer in your script? Commented Oct 11, 2014 at 5: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? Commented Oct 11, 2014 at 6: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 .. Commented Oct 11, 2014 at 6:23

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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

6 Comments

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..
The detailed description was added on same time when I posted the answer. So didn't know about that.
Yes, you are right, can you edit your answer to help me now ?
This link might help you :-stackoverflow.com/questions/10895312/…
Hey, I can't un-downvote the answer ! Can you please edit your answer to contain that ^ link
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.