Last active
August 29, 2015 14:01
-
-
Save dyoder/bfcee0204f5cb97cf7de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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