2014-05-20 85 views
0

我正在使用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变量作为关键字,以使所有内容尽可能动态。我知道如何用数据填充数组,但我无法弄清楚如何给它一个特定的键和值。

+0

您是否有样本/数据格式(由$ .get()返回)? – PeterKA

+0

它应该是'cache.push(data);' –

+0

@ user3558931这很难,因为结果来自wordpress中的文章。最后它只是段落标签内的文本。 – user1433479

回答

1

在Javascript中,大多数人会用一个简单的对象来模拟一个关联数组的行为。

首先,改变你的cache定义为一个空对象:

// Cache 
var cache = {}; 

然后,当你想保存加载HTML,只是这样做:

cache[page] = data; 

现在,虽然你不”不要指定,我认为你的逻辑中也有一个错误 - 你只有一个lastTime变量,但我认为你会为每个潜在的页面负载单独保留一个1分钟的计时器。在这种情况下,你可能有更多的东西是这样的:

cache[page] = { 
    html: data, 
    expires: new Date().getTime() + 60000 
}; 

要清楚,这里就是我可能会重写功能片段,使用cache对象来存储的HTML数据,并应到期时间。

// Cache 
var cache = []; 

function load_page(page, location) { 
    var now = new Date().getTime(); 

    // Check for a previously cached value 
    var result = cache[page]; 

    if (result && result.expires > now) { 
    // Use the cached HTML data 
    $(location + ' .homeText').html(result.data); 
    } else { 
    // Get URL 
    $.get('http://mydomain.com/get_posts.php?page=' + page, function (data) { 
     $(location + ' .homeText').html(data); 

     // Store the HTML and its expiration time 
     cache[page] = { 
     data: data, 
     expires: now + 60000 
     }; 
    }); 
    } 
} 
+0

我实际上必须切换接受的答案。我刚才遇到了这个bug,不知道如何解决它。 10/10再加一英里。 – user1433479

2

像这样的事情

var cache = {}; 

从缓存

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

得到要填充缓存

cache[page] = data; 
+0

伟大的解决方案。 – user1433479

0

有没有这样的事情在JavaScript关联数组。 Here是interessting博客文章有关:http://blog.kevinchisholm.com/...

但你可以做到这一点的想法:

cache.push({键:值,键:值});

0

另外,如果因任何原因,你不得不使用数组cache那么你可以使用下列内容:

// 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 
      var oPage = {}; 
      oPage[ page ] = data; 
      cache.push(oPage); 

      // Set lastTime var for checking how long since URL fetch 
      lastTime = new Date(); 

     }); 
    } 
}