user3628807
user3628807

Reputation: 325

Force page to reload a file if it's been changed

I will use it for different purposes but here is a good example of what I want to build but I don't know how to:

I have a .txt document that occasionally updates and I want to make a page that load and show that .txt file and if it's been changed to force that page to reload. ONLY if it's been changed. I don't want to make it to reload every X seconds cause that will kill the server.

How can I do it? Ajax probably but I don't know what function or plugin to use.

EDIT: And another good example is a notification box that updates only if you have new notifications.

Upvotes: 0

Views: 4375

Answers (1)

FrigginGlorious
FrigginGlorious

Reputation: 109

I'm having issues with the same thing. I have been trying something similar to this, but I think it's going to give issues with finding the ifModified heading on different file types?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">(function ajaxCall() {
console.log("working.")
$.ajax({
  type: "POST",
  url: "path/to/file.txt",
  ifModified: true,
  success: function () {
	console.log("file was updated, reload");
	location.reload();
	setTimeout(ajaxCall, 5000);  // Check again in 5 seconds?
  }
});
})();
</script>

Upvotes: 1

Related Questions