Hulu
Hulu

Reputation: 31

Run PHP page every hour automatically-without using Cron jobs

How can i run or refresh PHP page every 10 Minutes or an hour. so that add new record from one database to another i doesn't matter with using javascript but without using cron jobs.

Upvotes: 0

Views: 1045

Answers (2)

Jazzepi
Jazzepi

Reputation: 5480

You can depend on the traffic to your website if it's frequent enough. Drupal has something called poor man's cron. Basically you store the date-time that the cron job was last run. Each time a user visits a page on your website, you fetch the date-time, compare it with the current date-time to see if you need to run the cron job (in your case, see if an hour has passed). If the required amount of time has passed, then run your cron job, and store a new date-time. If your required amount of time hasn't passed, then do nothing.

This approach has a few caveats.

  • You really should be using cron, and not doing this. There's a reason it's called poor man's cron. If you're using some hosting service by someone else, their technical support should be able to help you setup a cron job. If the service has cPanel you can do it from cPanel. https://documentation.cpanel.net/display/ALD/Cron+Jobs
  • You are completely dependent on website traffic to trigger your cron job. You need to decide if it's OKay for your cron job to not be run for a period of time where users are not visiting your website.
  • You're slowing every request to your website down by a fetch to whatever persistent store is holding the last date-time the cron job was run.

Upvotes: 1

joseluiscc
joseluiscc

Reputation: 234

with javascript you can use the setTimeout() function but the user will need to keep the page opened.

i.e

setTimeout(function(){ your_process(); }, 360000); // every hour

Upvotes: 0

Related Questions