I'm working on an app that uses an html/javascript framework. The app loads in pages from a database using a PHP script and a $.get call whenever a page is shown like so:
// Set lastTime var for counting the amount of time passed since $.get
var lastTime = 0;
// Amount of minutes before new $.get
var timeOut = 1;
// Cache
var cache = [];
function load_page(page, location) {
// Check if 1 minute passed
if ( Math.floor((new Date() - lastTime)/60000) < timeOut ) {
// Get cache
$(location+' .homeText').html(cache[page]);
} else {
// Get URL
$.get('http://mydomain.com/get_posts.php?page='+page, function(data) {
$(location+' .homeText').html(data);
});
// Fill array with data
cache.push();
// Set lastTime var for checking how long since URL fetch
lastTime = new Date();
}
}
It's almost done but I can't figure out how to populate my cache variable.
I want to populate it in such a way that I can use the page variable as a key to keep everything as dynamic as possible. I know how to fill an array with data but I can't figure out how to give it a specific key and value.
cache.push(data);0,1and2instead of thepagevariable as a key.