I'm working on a live photo stream app. Essentially, users will be uploading photos to a folder on my server via FTP, and the page should update anytime a new photo is added without refreshing.
I plan to do this with AJAX and the method suggested in this thread: How to check if directory contents has changed with PHP?. Essentially, I want to have a loop on my page that every X seconds, makes an AJAX call to a PHP page which gives back the MD5 hash of the directory listing for the uploads folder. If the hash has changed since the last call, another AJAX call will get the most recently added file and jQuery will display it on the page.
In vanilla Javascript/jQuery, this can be done using a recursive, named function with a setTimeout inside of it. This code is working for me:
function refreshLoop(currentFolderState, refreshRate) {
// Get the new folder state
$.get("../ajax/getFolderState.php", function(data) {
// If it's different than the current state
if ( data !== currentFolderState ) {
// Do something
}
// If it's the same as the current state
else {
// Do nothing
}
// After the refresh rate, try again
setTimeout(function() {
refreshLoop(data, refreshRate);
}, refreshRate);
});
}
// Document Ready
$(function() {
var refreshRate = 5000;
// After refresh rate has passed
setTimeout(function() {
// Get the starting folder state
$.get("../ajax/getFolderState.php", function(data) {
// Kick off the loop
refreshLoop(data, refreshRate);
});
}, refreshRate);
});
I'm using Coffeescript on this project in an attempt to learn how it works, since a lot of developers seem to be fond of it, but I can't figure out how to replicate this functionality without the use of named functions. Can someone either point me in the right direction or explain a better way for me to achieve this effect that can be easily done in Coffeescript?