Jump to content

Executing a function every $n minutes


shlumph

Recommended Posts

Hey guys,

 

I'm writing a web related program in PHP, and need to execute a function every 10 (completely arbitrary) minutes. Is there a PHP function that will allow this? Do I need to use threads? Any help as to what to research, or links to code examples would be greatly appreciated.  :-*

Link to comment
https://forums.phpfreaks.com/topic/129493-executing-a-function-every-n-minutes/
Share on other sites

I'm using a shared host under GoDaddy, which provides a cron manager, but only allows the following frequency:

hourly, daily, weekly, and monthly.

 

I need a loops iteration to take about 4-20 minutes like this:

function getData($arrayItem) {
      //Do Stuff, Return Stuff
}

$data = array();

for($i=0; $i<200;$i++) {
     $data[] = getData($urlArray[$i]);
     //Pause for 4-20 minutes
}

 

And the whole loop must finish. So in the end, it's going to take a while for the program to execute. I basically want to slow down the loop. I could check time every iteration, and see if it's been 10 minutes, but I'm afraid I'll blow up my shared hosting server  ;)

 

 

Create a database that holds a time in it. At the top of all scripts, place a check to see if more than 10 minutes has passed since the time saved in the database. If it has, save the current time in the database, then run the scripts that need running every ten minutes.

 

This method won't work in exactly 10 minute intervals though, as the scripts won't be triggered until the next page access AFTER ten minutes has gone by. But if you can deal with that, then it's an easy way of doing this.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.