Javascript jQuery在函数外部使用变量

Javascript jQuery在函数外部使用变量,javascript,jquery,json,Javascript,Jquery,Json,我正在使用它来聚合rss提要并预处理接收到的数据。问题是我不能使用变量summary在preprocess函数之外设置的变量。我确实将它设置为一个通用变量,所以我不知道我可能做错了什么。运行多个JSON请求可能是个问题吗 我的代码: $('#feed').feeds({ feeds: { reuters: 'http://feeds.reuters.com/reuters/businessNews' }, max: 2, preprocess: f

我正在使用它来聚合rss提要并预处理接收到的数据。问题是我不能使用变量
summary
preprocess
函数之外设置的变量。我确实将它设置为一个通用变量,所以我不知道我可能做错了什么。运行多个JSON请求可能是个问题吗

我的代码:

$('#feed').feeds({
    feeds: {
        reuters: 'http://feeds.reuters.com/reuters/businessNews'
    },
    max: 2,
    preprocess: function ( feed ) {
        var articleLink = (this.link);
        var summarize = '';

        $.getJSON({
            url: 'https://jsonp.nodejitsu.com/?url=http://clipped.me/algorithm/clippedapi.php?url='+articleLink+'&callback=?',
            corsSupport: true, 
            jsonpSupport: true,

            success: function(data){
              var summarize = data.summary
            }
          });

          alert(summarize);

        this.contentSnippet = summarize

    },
    entryTemplate: '<h3><!=title!></h3><p><!=contentSnippet!></p><i><!=link!></i>'
});
$(“#提要”).feed({
提要:{
路透社:'http://feeds.reuters.com/reuters/businessNews'
},
最高:2,
预处理:函数(提要){
var articleLink=(this.link);
var='';
$.getJSON({
网址:'https://jsonp.nodejitsu.com/?url=http://clipped.me/algorithm/clippedapi.php?url=“+articleLink+”&回调=?”,
科斯波特:没错,
jsonpSupport:正确,
成功:功能(数据){
var summary=data.summary
}
});
警惕(总结);
this.contentSnippet=summary
},
entryTemplate:“

” });
我想你是这个意思

$('#feed').feeds({
    feeds: {
        reuters: 'http://feeds.reuters.com/reuters/businessNews'
    },
    max: 2,
    preprocess: function ( feed ) {
        var articleLink = (this.link);
        var summarize = '';

        var that = this;

        $.getJSON({
            url: 'https://jsonp.nodejitsu.com/?url=http://clipped.me/algorithm/clippedapi.php?url='+articleLink+'&callback=?',
            corsSupport: true,
            jsonpSupport: true,

            success: function(data){
                that.contentSnippet = data.summary
            }
        });

    },
    entryTemplate: '<h3><!=title!></h3><p><!=contentSnippet!></p><i><!=link!></i>'
});
$(“#提要”).feed({
提要:{
路透社:'http://feeds.reuters.com/reuters/businessNews'
},
最高:2,
预处理:函数(提要){
var articleLink=(this.link);
var='';
var=这个;
$.getJSON({
网址:'https://jsonp.nodejitsu.com/?url=http://clipped.me/algorithm/clippedapi.php?url=“+articleLink+”&回调=?”,
科斯波特:没错,
jsonpSupport:正确,
成功:功能(数据){
that.contentSnippet=data.summary
}
});
},
entryTemplate:“

” });
数学是正确的。做这个

$(“#提要”).feed({
提要:{
路透社:'http://feeds.reuters.com/reuters/businessNews'
},
最高:2,
预处理:函数(提要){
var articleLink=(this.link);
var='';
var_this=这个;
$.getJSON({
网址:'https://jsonp.nodejitsu.com/?url=http://clipped.me/algorithm/clippedapi.php?url=“+articleLink+”&回调=?”,
科斯波特:没错,
jsonpSupport:正确,
成功:功能(数据){
_this.contentSnippet=data.summary
}
});
警惕(总结);
},
entryTemplate:“


});您有一系列错误,这些错误在其他帖子中没有提到

  • 预处理
    回调允许在显示当前对象(提要)之前对其进行更改。
    由于getJSON是一个ajax调用,因此获取结果太晚。即使在
    success
    回调中更改
    contentSnippet
    也无法解决此问题
  • 使用
    $.getJSON
    方法就像使用
    $.ajax
    一样。所以你传递了错误的论点。只需使用
    $.ajax
    作为语法
  • 最后,为了解决第一个问题,您需要稍微修改一下模板,以便稍后(当ajax请求完成时)可以找到相关部分,并使用
    onComplete
    回调(而不是feeds插件)
所有的变化加在一起会产生

$('#feed').feeds({
    feeds: {
        reuters: 'http://feeds.reuters.com/reuters/businessNews'
    },
    max: 2,
    onComplete: function(entries){ // use onComplete which runs after the normal feed is displayed
        var $this = $(this);
        entries.forEach(function(entry){
            var $self = $this.find('.entry[data-link="'+entry.link+'"]');

            $.ajax({
                url:'https://jsonp.nodejitsu.com/?url=http://clipped.me/algorithm/clippedapi.php?url='+entry.link,
                corsSupport: true, 
                jsonpSupport: true,     
                success: function(data){
                    // add the results to the rendered page
                    $self.find('.snippet').html( data.summary );
                }
              });
        });   
    }, // change the template for easier access through jquery
    entryTemplate: '<div class="entry" data-link="<!=link!>"><h3><!=title!></h3><p class="snippet"><!=contentSnippet!></p><i><!=link!></i></div>'
});
$(“#提要”).feed({
提要:{
路透社:'http://feeds.reuters.com/reuters/businessNews'
},
最高:2,
onComplete:函数(条目){//使用在显示正常提要之后运行的onComplete
var$this=$(this);
entries.forEach(函数(entry){
var$self=$this.find('.entry[data link=“'+entry.link+'”);
$.ajax({
网址:'https://jsonp.nodejitsu.com/?url=http://clipped.me/algorithm/clippedapi.php?url=“+entry.link,
科斯波特:没错,
jsonpSupport:正确,
成功:功能(数据){
//将结果添加到呈现页面
$self.find('.snippet').html(data.summary);
}
});
});   
},//更改模板以便于通过jquery进行访问
entryTemplate:“

” });

演示'summary'变量你想用它做什么?我正在将'this.contentSnippet'的值更改为'summary'的值不,因为这是在异步
getJSON
完成之前发生的。啊,好的,到目前为止,我从未听说过异步getJSON。我只是尝试将“$this”编辑为“this”,但它不起作用。对不起,我不是有意添加$。更正了帖子。非常感谢你花时间研究你的答案。我真的很感激。这很好用