Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a recursive setTimeout loop in Coffeescript

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?

like image 998
Ryan Giglio Avatar asked Dec 04 '22 17:12

Ryan Giglio


2 Answers

A more succinct version

do poll = ->
  #do work here
  setTimeout poll, 1000

which compiles to

var poll;
(poll = function() {
  //do work here
  return setTimeout(poll, 1000);
})();
like image 112
kasperlanger Avatar answered Dec 07 '22 06:12

kasperlanger


You could do something like this in CoffeeScript:

refresh_loop = (data, refresh_rate) ->
    #...etc

refresh_rate = 5000
setTimeout((->
    $.get("../ajax/getFolderState.php", (data) ->
        refresh_loop(data, refresh_rate)
), refresh_rate)

Demo: http://jsfiddle.net/ambiguous/ZVTcg/

If your function was smaller then you could inline all of it like this:

refresh_rate = 5000
setTimeout(f = (->
    // real work goes here...
    setTimeout(f, refresh_rate)
), refresh_rate)

Demo: http://jsfiddle.net/ambiguous/XThV6/

Inlining all of it would probably be a bit ugly and confusing in your case though so using a separate refresh_loop = (data, refresh_rate) -> ... construct is probably a better idea.

like image 31
mu is too short Avatar answered Dec 07 '22 07:12

mu is too short