Skip to content

Instantly share code, notes, and snippets.

@dyoder
Last active August 29, 2015 14:01
Show Gist options
  • Save dyoder/bfcee0204f5cb97cf7de to your computer and use it in GitHub Desktop.
Save dyoder/bfcee0204f5cb97cf7de to your computer and use it in GitHub Desktop.
load_product_catalog = (url, frequency, callback) ->
data = {} # keep the data for the catalog
json = "" # keep the raw JSON
# load the product catalog, possibly from cache
# the browser will add the caching request headers for us
load = ->
request = new XMLHttpRequest()
request.open "GET", url, true
request.onload = update
request.send()
# response handler - we may be getting the responseText
# from cache, which is why we keep the JSON around
update = ->
# don't parse the JSON unless it's changed
if this.responseText != json
json = this.responseText
data = JSON.parse json
callback data
# reload the catalog every minute
setTimeout load, frequency
# do the initial load so there's no delay the first time
# someone tries to do a lookup
load()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment