/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Run or schedule script/function to run one hour after provided time.

How can I schedule a function to run one hour after a specified time?

[CODE]
var custom_time = “2013-11-09T19:06:27-06:00”;
schedule(custom_time, 60, script());
function script() {
// Would run code here after 60 minutes
}
[/CODE]

Of course, “schedule()” doesn’t exist, I just put this here to show what I want to accomplish. I have MomentJs already in use on my site, and you can use it to manipulate the time if you need to. The provided time is in a ISO-8601 format (so it is compatible with MomentJs).

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@AlexAtNetNov 10.2013 — You can use [FONT=Courier New]setTimeout[/FONT] function:

<i>
</i>function script() {
// Would run code here after 60 minutes
}

var custom_time = "2013-11-09T19:06:27-06:00";
setTimeout(script, new Date().getTime() - Date.parse(custom_time) + 60 * 60 * 1000);
Copy linkTweet thisAlerts:
@DaAwesomePauthorNov 10.2013 — I understand the code, but when I use just the math part:
[CODE]
new Date().getTime() - Date.parse(custom_time) + 60 * 60 * 1000
[/CODE]

and I input [FONT=courier new][/FONT][FONT=courier new]2013-11-09T22:01:01-06:00[/FONT], after formatting it it returns sumwhere around 7:30pm when accessing it at about 10:30pm. It should calculate that it needs to reload at 11:01pm. Which would be about 30 minutes.
Copy linkTweet thisAlerts:
@AlexAtNetNov 10.2013 — How do you format it? For me it returns different time because of the different timezone:

<i>
</i>new Date(Date.parse('2013-11-09T22:01:01-06:00')).toString()
// "Sun Nov 10 2013 04:01:01 GMT+0000 (GMT Standard Time)"
Copy linkTweet thisAlerts:
@DaAwesomePauthorNov 10.2013 — I was formatting it using MomentJS.

I just figured out that I can do this:
[CODE]
var custom_time = "2013-11-09T19:06:27-06:00";
moment_now = moment(new Date());
moment_customtime = moment(custom_time).add('h', 1);
moment_runscripttime = moment_customtime.diff(moment_now);
setTimeout(script, moment_runscripttime);
[/CODE]

It works! I think it's offset by 1 minute after, but I don't care about that. I could fix that if I wanted to. MomentJS is becoming more and more helpful.
×

Success!

Help @DaAwesomeP spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...