从jQuery函数中返回值 [英] Returning value from a jQuery function

查看:123
本文介绍了从jQuery函数中返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么这个jQuery函数没有从函数返回任何值。它确实增加了Firebase的价值,除了返回值之外,其他一切工作都正常。 (


$ b

  $(function(){
$('。post')。每个(函数(){
var $ this = $(this);
var postTitle = $ this.find('。title')。text();

/ /创建单独的firebase引用
var postRef = new Firebase('https://bloganalyzer-view-stat.firebaseio.com/trying/'+postTitle);

var pData = getFirebaseData( ('。count')。empty()。html(pData);
});
});

函数getFirebaseData(r,bp){
var data;
r.once('value',function(snap){
data = snap.val(); $ b $ (data == null){data = 1;}
else if(bp =='blog'){data + = 10;}
else if(bp =='post'){data + = 1;}
r.set(data);
});
返回数据;
}

和HTML部分就是这样的东西..

 < div class =post> 
< span class =title> Title 1< / span& gt;
< span class =viewCount>< / span>
< / div>
< div class =post>
< span class =title>标题2< / span>
< span class =viewCount>< / span>
< / div>

任何形式的帮助都将被赞赏。


  $(function(){
$('。post')。
var postTitle = $ this.find('。title')。text();

//创建单独的firebase参考
var postRef = new Firebase('https:/ /bloganalyzer-view-stat.firebaseio.com/trying/'+ postTitle);

//将回调传递给getFirebaseData,一旦请求完成,就会调用getFirebaseData
getFirebaseData(postRef, 'post',function(pData){
//一旦请求完成,就会调用它
$ this.find('。count')。html(pData);
}) ;
});
});
//接受回调函数作为函数的最后一个参数
函数getFirebaseData(r,bp,callback){
r.once('value',function(snap){
var data = snap.val();
if(data == null){
data = 1;
} else if(bp =='blog'){$ b $ (数据);



$ b //一旦计算出数据的值,就调用回调并将值传递给它,而不是试图返回它
回调(数据)
});




I was wondering why this jQuery function is not returning any value from the function. It does increase the value in Firebase and everything else works fine except returning value. Badly stuck with this for hours :(

 $(function(){
  $('.post').each(function(){
    var $this = $(this);
    var postTitle = $this.find('.title').text();

    //create separate firebase reference
    var postRef = new Firebase('https://bloganalyzer-view-stat.firebaseio.com/trying/'+postTitle);

    var pData = getFirebaseData(postRef,'post');
    $this.find('.count').empty().html(pData);
  });
});

function getFirebaseData(r,bp){
  var data;
  r.once('value', function(snap) {
    data = snap.val();
    if (data == null) {data=1;}
    else if(bp=='blog') {data+=10;}
    else if(bp=='post') {data+=1;}
    r.set(data);
  });
  return data;
}

and the HTML part is something like that..

<div class="post">
  <span class="title">Title 1</span>
  <span class="viewCount"></span>
</div>
<div class="post">
  <span class="title">Title 2</span>
  <span class="viewCount"></span>
</div>

Any kind of help would be appreciated.

解决方案

The firebase api is an asynchronous api, so you can't return value frrm that instead you can use a callback to do the processing

$(function () {
    $('.post').each(function () {
        var $this = $(this);
        var postTitle = $this.find('.title').text();

        //create separate firebase reference
        var postRef = new Firebase('https://bloganalyzer-view-stat.firebaseio.com/trying/' + postTitle);

        //pass a callback to getFirebaseData which will be called once the request is completed
        getFirebaseData(postRef, 'post', function (pData) {
            //this will get called once the request is completed
            $this.find('.count').html(pData);
        });
    });
});
//accept the callback as the last param which is a function
function getFirebaseData(r, bp, callback) {
    r.once('value', function (snap) {
        var data = snap.val();
        if (data == null) {
            data = 1;
        } else if (bp == 'blog') {
            data += 10;
        } else if (bp == 'post') {
            data += 1;
        }
        r.set(data);
        //once the value of data is calculated call the callback and pass the value to it instead of trying to return it
        callback(data)
    });
}

这篇关于从jQuery函数中返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆