Timed code execution loop - Javascript?

Soldato
Joined
6 Jun 2011
Posts
2,739
Hi guys,

I am trying to plan the best way of coding a PHP file. Basically I have a function that I want to run for a certain period of time and then repeat again with different parameters in a continuous loop. What would be the best way of doing this?

Thanks :)
 
Associate
Joined
20 Jul 2011
Posts
128
Location
London, UK
You can set up tasks to run at regular intervals on the server using cron (linux) or scheduled tasks (windows) (If you have sufficient privileges on the server, which might not be the case if you don't own the server).

http://www.thegeekstuff.com/2011/07/php-cron-job/

You create a php script which runs the function, which is then set up to run via the php executable i.e.:

php yourscript.php

which is run as a cron job / scheduled task on the server.

If you can't run the script as a server task, you might have to use a web page to trigger the script via a repeated javascript ajax call. You would need to create a javascript function to call the php script via AJAX, and then run this function at regular(ish) intervals using the javascript setInterval function
 
Last edited:
Associate
Joined
20 Jul 2011
Posts
128
Location
London, UK
Do you mean the function is defined in one file, and called in another via an include/require statement?

Also when you say you want to run the fuction once with one set of parameters and then run again with different parameters, do you mean each time the script runs it will run the function with a unique set of parameters for each call?
 
Soldato
OP
Joined
6 Jun 2011
Posts
2,739
Sorry I probably didn't make myself that clear.

Basically I want to have one PHP file which then uses a function in another file. I want to run the function with one set of parameters for say 2 minutes then after this define the function again with different parameters and run this for say 2 minutes.

Thanks :)
 
Soldato
Joined
3 Jun 2005
Posts
3,119
Location
The South
As Oz!asM!dwinter mentioned, you can schedule to execute a task at specific time/date/interval but it won't restrict execution length of said task.

If you need to execute it for X amount of time and assuming the 'primary' (the function doing the processing/work, not the function or loop doing the parameter changes) function is loop based then you'd need to rewrite the function with time-checks ie - grab time at start of execute, at every loop check to see how much time has past since execute, if over X amount of time the exit loop.

As said, might be better to give us some hint at what you're exactly doing.


Edit - You could potentially use the 'set_time_limit' and 'max_execution_time' PHP limits and then schedule the function to run every 2 minutes but personally that seems a bit 'bodgy' to me.
 
Last edited:
Back
Top Bottom