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
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  ;)

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.