我有与动态内容的div:检查时间戳localStorage的记录
<div id="news">test</div>
并希望添加其内容的localStorage 50秒,以防止对服务器的新的要求,如果是年龄超过50秒然后删除它并设置一个新的内容。我jQuery是:
$(function(){
function now() {return+new Date}
var db = window.db = {
get : function(key) {
var entry = JSON.parse(localStorage.getItem(key)||"0");
if (!entry) return null;
if (entry.ttl && entry.ttl + entry.now < now()) {
localStorage.removeItem(key);
return null;
}
return entry.value;
},
set : function(key, value, ttl) {
localStorage.setItem(key, JSON.stringify({
ttl : ttl || 0,
now : now(),
value : value
}));
}
};
});
$(function() {
// Set Value with TTL of 50 Seconds using Milliseconds.
db.set("homeNews", $("#news").html(), 50000);
});
$(function() {
var contentsOfNews = db.get("homeNews");
$("#news").html(contentsOfNews);
});
的问题是:这是每个负荷增加的内容到localStorage的,但应该检查时间戳,如果加入的含量大于50秒,然后重写这个localStorage的数据。如果少于50秒,则不做任何事情。感谢您的帮助。
请随意编辑小提琴:http://jsfiddle.net/KQmky/1/
万分感谢
使用[setTimeout()](http://www.w3schools.com/jsref/met_win_settimeout.asp)函数 – Pavlo
谢谢帕夫洛,对不起,因为愚蠢,但你能请帮助这在我的jsfiddle?谢谢 – qqruza
'''setTimeout(function(){db.set(“homeNews”,$(“#news”)。html(),50000);},50000);' – Pavlo