0

I'm developing website where I need to execute one code at particular time.

Which is faster and better choice to write Cron Job or to use JavaScript Timing Event or something similar or JavaScript

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
CBeTJlu4ok
  • 1,072
  • 4
  • 18
  • 51

4 Answers4

4

You are asking a question about two completely different things.

Cron job is based on the server, JavaScript (unless you are using NodeJS) is based on the client. Depending on whether this is a task that:

  • must be performed and cannot be relied on the client (eg. the data is sensitive), or
  • can depend on the client execution (which means the browser window should remain open and the JavaScript should be enabled),

choose Cron (1) or JavaScript (2) respectively.

It is really like a comparison between apples and oranges. Unless you will tell us whether you want orange juice or apple pie, we won't be able to help you more. Just remember that Cron is for more reliable server-side task execution, and JavaScript timeout is per-user (or rather per-client), less reliable execution.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • I see, I thought about sime timer in Javascript. what about node.js? is it more efficient? I'm just trying to find some alternative to cron job – CBeTJlu4ok Sep 14 '12 at 23:53
  • @Mpa4Hu: You should rather tell us what kind of code / task you are trying to execute. See my answer, the questions from it are still not answered. But there are some alternatives to cron job (depending, of course, what kind of task you are trying to execute). – Tadeck Sep 14 '12 at 23:59
  • Why do you need an "alternative" to cron? – Winfield Trail Sep 14 '12 at 23:59
  • query from database to get latest id in table, random method from 1 to the latest id, and then insert it to another table. – CBeTJlu4ok Sep 15 '12 at 00:03
  • I'm searching for "alternatvie" just to not choose from one. – CBeTJlu4ok Sep 15 '12 at 00:04
  • 1
    @Mpa4Hu: If you are trying to achieve something server-side (not per-user), then server-side solutions are the best. It seems this is the case now. To achieve effect similar to cron, but without cron, you can follow eg. [tips from this answer](http://stackoverflow.com/a/7939687/548696) (this way cron's task is performed eg. by [Drupal's poormanscron module](http://drupal.org/project/poormanscron)). – Tadeck Sep 15 '12 at 01:04
2

It entirely depends on the nature of the code you need to execute at a particular time.

If it's something that has to happen every day at 2pm or whatever, regardless of whether or not anyone's looking at the website, then you should use a cron job for that.

On the other hand, if it's something that needs to execute at a certain time per user (i.e., to automatically log a user out of a page after some amount of idle time), then the appropriate call is Javascript timing functions.

Javascript timing functions will only work if someone's actually looking at the page, and then it'll be called multiple times for multiple users, which may or may not be desirable depending on your situation.

Of course, you may be running Node.js on the server, in which case you can use Javascript timing functions as if they were a cron job.

Ethan Brown
  • 26,892
  • 4
  • 80
  • 92
1

In short, use cron

In your comment to another answer, you said:

I want to execute php function every week one time

In this case, you have one main option (assuming you are using *nix) and that is cron (I don't know what the Windows alternative is). Cron is specifically designed for this function, and whether or not you choose to use it, it is most likely running on your server anyway (for other system functions) so speed is not an issue.

Don't use Node.js

Node.js is an alternative serverside technology to PHP. You would use it server side instead of php. If you're already using PHP, then forget it. The only reason Node.js has been mentioned is because you've asked about JavaScript.

Also, for a weekly timing event, A JavaScript timer wouldn't be a good idea. The setTimeout() function works in milliseconds, and is good for working in seconds and minutes (possibly hours), but not weeks.

If you were to use serverside JavaScript (like Node.js), you would probably need to do something similar to the PHP Alternative below.

PHP Alternative

Of course, depending on your hosting environment (especially cheaper ones), cron may not be available. In this case you would have to come up with a different strategy, and you would probably be best to use PHP. Something that I've seen done before goes along these lines:

  1. Have some register of jobs that need to be performed. (In a database, or a file, or whatever)
  2. Every time you run your main PHP script (usually index.php), check the register to see if there are any outstanding jobs. 2.a. Run the job. 2.b. Update the register, so you remember the last time the job was performed.

Pros:

  1. It works if you don't have access to cron.

Cons:

  1. If your script is not run very often (because this method relies on people visiting your page), your jobs may not be run as often as you like.
  2. If your script is run very often, you will suffer unnecessary overhead in your script.
  3. If your jobs take a long time to run, it will effect the page load times.
  4. You're basically replicating cron, but using PHP which is far less efficient than using cron.
  5. It's unlikely (unless you invest a lot of time) that you'll develop a solution that is as good as cron.
Luke Mills
  • 1,616
  • 1
  • 11
  • 18
0

For a javascript timing event to run you would need to open the webpage. That means you have to expose that page publicly. You don't want to do that. Cron jobs are easy and effective. I like them. You should do that.

ajon
  • 7,868
  • 11
  • 48
  • 86