Skip to content

Instantly share code, notes, and snippets.

@gockxml
Created November 26, 2012 09:24
Show Gist options
  • Save gockxml/4147359 to your computer and use it in GitHub Desktop.
Save gockxml/4147359 to your computer and use it in GitHub Desktop.

Revisions

  1. gockxml created this gist Nov 26, 2012.
    33 changes: 33 additions & 0 deletions bind_ajax_update.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    function bind_ajax_update(selector, until, interval, get_status, callback){
    var filters = []
    interval = interval || 5000
    for (var i in until){
    filters.push("[status=" + until[i] + "]")
    }
    var ajax_update = function(elem){
    var $elem = $(elem)
    var url = $elem.attr("data-url")
    console.log("ajax update")
    $.ajax({
    url : url,
    success : function(data){
    var status = get_status ? get_status.call(null, data) : data
    var finished = until.indexOf(status) >= 0
    callback = callback || $elem.html
    callback.call($elem, status, elem, finished, data)
    if (!finished){
    setTimeout(function(){
    ajax_update(elem)
    }, interval);
    }
    }
    })
    }
    $(document).ready(function(){
    //console.log($(selector).filter(filters.join(",")))
    $(selector).each(function(){
    ajax_update(this)
    })
    })

    }