Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Run .js file every 15. minute

I have a .js file that is on my web-page. It is scraping another web-page and if the information is edited I do something. My problem is that I don't know to to run the javascript file. I know how to do it with a php-script, with cron-jobs, but I can't find a way for javascript, so my question is:

  1. Is it possible to run a .js file from php?

  2. Is there a service like cron-job for javascript(only free services)

6 Answers

Max Dubinin
Max Dubinin
12,807 Points

No. What you're talking about is a thread. You'll need a server for that. If you want to implement that in JS then read about Node.js multithreading

Yes, I have a server, but I don't know how to implement Node.js to the server(web-page), is there any good documentation on that?

Max Dubinin
Max Dubinin
12,807 Points

There is an entire course about node.js here on treehouse and I strongly recommend it

Does that show how to use it on my own web-page to?

Max Dubinin
Max Dubinin
12,807 Points

A server is not something that is running on a web-page. It's a separate process... There are two sides to web development: Front-End and Back-End. Your web-page is Front-End (the face of the website). The server is the Back-End and it's all the "brain" and the logic that is behind it. You should really take the whole Full Stack JavaScript track, it's really good! Then you'll understand everything.

Jim Dennis
Jim Dennis
13,075 Points

When you say that you have some JavaScript "that is on my web-page" that suggests that it's being run by your browser when you visit that page (the normal JavaScript execution context for most people).

It looks like you're saying that this script is checking some date/time stamp in the current page and, if that's more than 15 minutes ago (1500000 milliseconds), then you have your code pulling data from a different page (and merging it into your document?).

Of course you can do that on page load; and also set a timer for the check to recur periodically while the page is sitting there, open, in a browser.

This will be somewhat inefficient over time (each of the clients will be executing the same code and performing the same additional data fetches and merging). So I'm guessing (especially given your reference to PHP) that you're hoping to devise some way to update the server's storage of the web page which is hosting this JS script. For that you need something on the server side.

Others in this thread have suggest that you could use NodeJS to host a different script on the server. Your client side JS code would then hit some controller/route on your NodeJS server and trigger a script which fetches the new data and stores it to the the filesystem (or database, or whatever is storing the source data for the original page which is hosting your current code).

Does that make sense?

Max Dubinin
Max Dubinin
12,807 Points

http://www.w3schools.com/jsref/met_win_setinterval.asp I think setInterval() is what you need for the timing purpose. For running the .js file I think you'll manage. If not, feel free to reply

Does this work tough there is no windows open on the page?