tags:

views:

82

answers:

3

in javascript how to send request some url at a partcular time(e.g daily 5 pm).........

very day @5pm i have to send some request (url)e.g sending emails how to do in java script

+2  A: 

You really don't want to do it this way. If you have access to the server take a look at scheduled tasks (for windows) or cron jobs (for linux).

If you absolutely have no other alternatives the only way to accomplish this would be to create a loop with setInterval and check the current time in every iteration. But I just cringed writing that.

Chris Pebble
`setInterval` not `setTimeout`
David Dorward
Good point. Updated.
Chris Pebble
my main requirement is sending mails daily exactly @ 5pm ....@5 i it should trigger or call the mail sending function......
dpaksp
A: 

I am going to hazard an answer anyway:

setTimeout(function(){
    //payload here
}, 3600000*24); //one day. Launch at 5pm :)

This is quite simple and ugly, you probably want to add more detail to the condition (minutes, etc)

Victor
With the right timing this might skip the whole hour completely, by ticking in just before 17:00 and then due to inaccuracies in the timing system in the OS tick in just after 18:00, or it might end up ticking twice, just after 17:00 and just before 18:00.
Lasse V. Karlsen
I know. let me fix it :)
Victor
+2  A: 

Another solution:

var now = new Date();
// 17 o'clock today
var target = new Date(now.getYear(), now.getMonth(), now.getDay(), 17);

setTimeout(function(){
    // do stuff  
}, target.getTime() - now.getTime());

But regarding your question this is not a convenient solution. It means that you have to have the JS code running the whole day (with my example you even have to reload the code every day). A cron job is really a much more appropriate solution. There are also free cron job services on the internet.

Felix Kling