JSFiddle - React, Tailwind, and code Playground

by darcyclarke

HTML

<div></div>

JavaScript

xhttp = new xhttp(); // use your XMLHTTPRequest library of choice!
function setCache(n,v){
    var respCode = jsoc.set(n, v);
    alert('Data containing ' + n + ' was fetched and cached!');
}
 
function clearCacheItem(n){
    var respCode = jsoc.remove(n);
    alert('Data containing ' + n + ' is no longer cached.');
}
 
function cacheStuff(cName){
    jsoc = new JSOC(); // JavaScript Object Cache
    if(jsoc.get(cName) == undefined){
      // this object is not cached, so grab it as usual, then cache it via your call-back function.
      cacheName = cName; // set the global cacheName so that it's in scope for the call-back function.
      loc = 'http://dev.webframeworks.com/assets/getMyData.txt';
      xhttp.fetch(loc, 'responseProxy', {'method':'POST','type':'text'});
    }else{
      // grab your stuff from cache!
      var cacheItem = eval(cName);
      alert(cName + ' = ' + cacheItem);
    }
}
 
function responseProxy(response){
    var x = eval("("+ response+")");
    setCache(cacheName, cacheName, x.articles[17].article.body); // this is where cache is set as mentioned above.
}