从jQuery函数中返回值

编程入门 行业动态 更新时间:2024-10-14 00:27:47
本文介绍了从jQuery函数中返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

) $ b

$(function(){ $('。post')。每个(函数(){ var $ this = $(this); var postTitle = $ this.find('。title')。text(); / /创建单独的firebase引用 var postRef = new Firebase('bloganalyzer-view-stat.firebaseio/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/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('bloganalyzer-view-stat.firebaseio/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('bloganalyzer-view-stat.firebaseio/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) }); }

  • How do I return the response from an asynchronous call?

更多推荐

从jQuery函数中返回值

本文发布于:2023-11-26 02:31:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632297.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   返回值   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!