我正在使用html/javascript框架的应用程序。 在使用PHP脚本和$不用彷徨的呼叫从一个数据库页的应用程序加载页面时表示,像这样:用键+值填充javascript数组
// 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();
}
}
它几乎做,但我无法弄清楚如何填充我的cache
变量。 我想以这种方式填充它,以便我可以使用page
变量作为关键字,以使所有内容尽可能动态。我知道如何用数据填充数组,但我无法弄清楚如何给它一个特定的键和值。
您是否有样本/数据格式(由$ .get()返回)? – PeterKA
它应该是'cache.push(data);' –
@ user3558931这很难,因为结果来自wordpress中的文章。最后它只是段落标签内的文本。 – user1433479