gpt4 book ai didi

javascript - 用键+值填充javascript数组

转载 作者:行者123 更新时间:2023-11-28 19:49:26 24 4
gpt4 key购买 nike

我正在开发一个使用 html/javascript 框架的应用程序。当页面显示时,应用程序使用 PHP 脚本和 $.get 调用从数据库加载页面,如下所示:

    // 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 变量作为键来保持所有内容尽可能动态。我知道如何用数据填充数组,但我不知道如何为其指定特定的键和值。

最佳答案

类似这样的事情

var cache = {};

从缓存中获取

if (cache[page] !== undefined) {...

填充缓存

cache[page] = data;

关于javascript - 用键+值填充javascript数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23756663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com