JSFiddle - React, Tailwind, and code Playground
by lgedeon
JavaScript
var TC_Live_Cache = {
values: [],
callbacks: [],
timer: setInterval(function(){TC_Live_Cache.check();},6000),
getValue: function (key) {
return this.values[key];
},
setCallback: function (key, callback ) {
alert(key);
alert(callback);
if (typeof this.callbacks[key]=="undefined") {
this.callbacks[key] = jQuery.Callbacks( "unique" );
}
this.callbacks[key].add(callback);
},
check: function () {
/* TC_Live_Cache.values = {name:"John",time:"2pm"}; */
//alert('howdy');
jQuery.post(
'http://techcrunch.dev/?live_cache_check=1',
{ "live_cache_check": "1" },
function(data){
alert(data);
jQuery.each(data, function(index, value) {
console.log(index + ': ' + value);
console.log( TC_Live_Cache.values[index] );
});
jQuery.each( data, function(index, value) {
if (TC_Live_Cache.values != value) {
alert(value);
alert(TC_Live_Cache.values[index]);
}
});
TC_Live_Cache.values = data;
},
"json"
);
//TC_Live_Cache.callbacks['time'].fire("sleepy");
},
stopTimer: function () { //todo cancel callback instead of stopping timer.
clearTimeout(TC_Live_Cache.timer);
}
};
//usage
TC_Live_Cache.setCallback('time', function(value) {
alert(value);
//TC_Live_Cache.stopTimer();
});