javascript异步和twitter的问题
Issues with javascript async and twitter
我目前有代码和函数(如下所示),用于获取tweet,然后查看tweet数据以返回集合中最常用的单词。我的问题是如何以异步方式运行代码?到目前为止,我已经尝试过getTweets().done(getTopwords());
,但它只是返回undefined,我不确定这是我的代码有问题,还是没有达到预期的效果。每个单独的功能都有自己的功能。
编辑:
我的理解是getTweets().done(getTopwords());
调用getTweets()
,当它完成时调用getTopwords()
。
代码:
// placeholder variables
var profile = 'MCFC',
keyword = '',
count = 10,
date = '2015-11-11',
lan = 'en',
search = keyword + " since:" + date + " lang:" + lan,
tweettxt = [],
users = [];
// function for searching through twitter using the specified data
function getTweets(){
return client.get('search/tweets', { q: search, count: count, from: profile },
handleTweets)
}
// function for handling the tweets
function handleTweets(err, data){
if (err) {
console.error('Get error', err)
}
else {
return sortTweets(data);
}
}
// function for getting the frequency of each word within a string
function getFreqword(){
var string = tweettxt.toString(), // turn the array into a string
changedString = string.replace(/,/g, " "), // remove the array elements
split = changedString.split(" "), // split the string
words = []; // array for the words
for (var i=0; i<split.length; i++){
if(words[split[i]]===undefined){
words[split[i]]=1;
} else {
words[split[i]]++;
}
}
return words;
}
// function for returning the top 20 words from getFreqword()
function getTopwords(){
var topwords = getFreqword(),
toptwenty = [];
for (var i=0; i<=20; i++){
toptwenty.push(topwords[i])
toptwenty.sort(function(a, b){return a-b});
}
return toptwenty
}
编辑2:
// function for sorting through the tweets to return relevant information
function sortTweets (data) {
for (var indx in data.statuses){
var tweet = data.statuses[indx];
tweettxt.push(tweet.text); // push the tweet text so it can be sorted for the most frequent words
users.push(tweet.user.screen_name); // push the twitter user screen name so it can be sorted to find the most frequent users
}
}
试试这个;)
// placeholder variables
var profile = 'MCFC',
keyword = '',
count = 10,
date = '2015-11-11',
lan = 'en',
search = keyword + " since:" + date + " lang:" + lan,
tweettxt = [],
users = [];
// function for searching through twitter using the specified data
function getTweets(){
client.get('search/tweets', {
q: search,
count: count,
from: profile
},
handleTweets);
}
// function for handling the tweets
function handleTweets(err, data){
if(err){
console.error('Get error', err)
}
else{
sortTweets(data);
/* call getTopwords() as we sorted tweets */
toptwenty = getTopwords();
/* now you can access toptwenty */
console.log(toptwenty);
}
}
// function for getting the frequency of each word within a string
function getFreqword(){
var string = tweettxt.toString(), // turn the array into a string
changedString = string.replace(/,/g, " "), // remove the array elements
split = changedString.split(" "), // split the string
words = []; // array for the words
for(var i = 0; i < split.length; i++){
if(words[split[i]] === undefined){
words[split[i]] = 1;
}else{
words[split[i]]++;
}
}
return words;
}
// function for returning the top 20 words from getFreqword()
function getTopwords(){
var topwords = getFreqword(),
toptwenty = [];
for(var i = 0; i < 20; i++){
toptwenty.push(topwords[i])
toptwenty.sort(function(a, b){
return a - b
});
}
return toptwenty
}
// function for sorting through the tweets to return relevant information
function sortTweets(data){
for(var indx in data.statuses){
var tweet = data.statuses[indx];
tweettxt.push(tweet.text); // push the tweet text so it can be sorted for the most frequent words
users.push(tweet.user.screen_name); // push the twitter user screen name so it can be sorted to find the most frequent users
}
}
getTweets();
相关文章:
- 是否有其他人在设计Twitter Typeahead时遇到问题'的搜索栏
- Twitter引导程序旋转木马跳过问题
- Twitter Bootstrap药丸的问题
- javascript异步和twitter的问题
- Vis图形库,Twitter引导的响应式设计问题
- 实现 Twitter 引导手风琴时遇到问题
- 使用 Twitter 引导程序 3 的网站出现 IE11 导航问题
- Twitter 引导工具提示弹出框显示问题
- twitter小部件包装问题
- 问题显示twitter状态/user_timeline MVC3.Net
- Twitter引导程序:修复用户滚动经过导航栏时导航栏顶部的问题
- 将twitter api1.0迁移到1.1 json数据访问问题
- Twitter Bootstrap + Mootools + JQuery.NoConflict()问题,而试图使用弹出
- Twitter登录Oauth问题使用Jquery
- 关闭和打开Twitter引导情态快速导致问题
- Rails自定义Twitter引导模式删除方法,回调问题
- 有问题旋转facebook, twitter和youtube图标图像
- Twitter Feed与Ember Views的问题
- 在使用wordpress的twitter小部件中遇到CSS问题
- 使用Twitter Bootstrap (JS和Jquery)的下拉菜单不会下拉-这是什么问题?