我有一个php脚本,以JSON格式获取特定用户的最新推文。JSON被解析成一个AJAX函数,该函数在页面上的ID中显示最新的tweet。每隔x秒设置一个计时器来执行上述操作,无需用户刷新页面即可检索最新的tweet。这个可以完美地工作。
我现在正在尝试创建一个通知功能,当有新的tweet要显示时,它会显示通知。在检索到的JSON中,每条tweet都有一个名为'id_string‘的唯一ID。我想做的是以某种方式存储生成的id_string的值,然后每次请求检索新的tweet时,检查新的'id_string‘是否与存储的不同,如果不同,则显示通知。
有没有什么好的建议呢?!我研究过本地存储,但我对此不是很熟悉,据我所知,您不能在本地存储中检查字符串与其他字符串。
以下是帮助解释我的问题的必要代码:
向推特发出请求并生成JSON格式(tweets.php)的输出:
require_once('config/FrameFunctions.php');
$output = array();
foreach ($tweeters as $i => $tweeter){
$theTweeter = new Tweeter($tweeter, $tmhOAuth);
$allinfo = $theTweeter->getTweets();
$output[$i] = $allinfo;
}
header("Content-type: application/json");
echo json_encode($output);
每隔x秒发出请求并生成客户端输出的javascript:
$(document).ready(function() {
$('.fancybox').fancybox();
/*
* call ajax function and update latest
*/
var refreshTweets = function() {
console.log("updating..");
$.ajax({url:"tweets.php",success:function(result){
tweets = eval(result);
for(i=0;i<tweets.length;i++){
$("#latesttweet"+(i+1)).html(
tweets[i][0].user.name + ": " + tweets[i][0].text
);
}
}});
}
refreshTweets();
//set the time in milliseconds here for each refresh
setInterval(refreshTweets , 30000); //Interval
});
发布于 2013-03-16 23:06:09
这是一个基于我上面的评论的解决方案
/*
NOTE : allTweets will have to be populated with all the tweets ids on the page prior
to being called in refreshTweets so that it does not trigger fake notifications
*/
var allTweets = Array();
$(document).ready(function() {
$('.fancybox').fancybox();
/*
* call ajax function and update latest
*/
var refreshTweets = function() {
console.log("updating..");
$.ajax({url:"tweets.php",success:function(result){
tweets = result;
for(i=0;i<tweets.length;i++) {
//check if its a new tweet or not
if (allTweets[tweets[i][0].id_string] === undefined) {
allTweets[tweets[i][0].id_string] = 1;
//trigger notification here!
}
$("#latesttweet"+(i+1)).html(
tweets[i][0].user.name + ": " + tweets[i][0].text
);
}
}});
}
refreshTweets();
//set the time in milliseconds here for each refresh
setInterval(refreshTweets , 30000); //Interval
});
发布于 2013-03-16 23:08:18
我想和其他人一起评论,但我想我太新了。
无论如何,如果您没有存储敏感信息,您可以将数据存储在JSON中,对于较小的文件,PHP可以快速写入和读取这些数据。如果要存储大量数据,则可能需要考虑将值保存到MySQL数据库或类似的数据库中。
设置JSON存储和MySQL数据库在这里似乎都有很好的文档记录。
https://stackoverflow.com/questions/15449364
复制相似问题