我正在使用 jQuery,但我的问题是即使我在 .ajaxStop 回调函数中使用“page += 1”,我的 page 变量也被递增了几次,因为它在第一次执行后被执行了不止一次它被使用了。我将该变量用作传递给 Flickr API 的参数,以获取特定页面的数据。
发生的事情是第一次调用该函数时,回调函数被执行一次。然后我从“更多”按钮调用相同的函数以获得下一组结果但是这次函数被调用两次,下一次被调用三次,依此类推......这意味着我可以获得第 1 页, 2、4、7、11 等...
我调用的 AJAX 函数基本上是 .getJSON 函数和一些在其回调方法 [inside getPhotos(id)] 中调用的额外 .getJSON 函数
// This gets the user ID from a given Flickr user page URL and does some presentation stuff
function getUserID() {
$("#moreRow").hide(350);
var usr = document.getElementById('user').value
var Req_addr = 'http://api.flickr.com/services/rest/?method=flickr.urls.lookupUser&api_key=' + API_key + '&url=http%3A%2F%2Fflickr.com%2Fphotos%2F' + usr + json
$.getJSON(Req_addr, function(data) {
// Once the user is known, data about its photos is requested
getPhotos(data.user.id)
});
// This hides the user data panel
$("#userInfo").hide(0);
// This hides the settings panel
$("#settings").hide(0, function() {
$("#loader").slideDown(750);
});
// This is what displays the photos when all of the AJAX requests have received their responses (ajaxStop)
$("#photos").ajaxStop(function() {
// the page counter is incremented for the next page to be requested next time
page += 1
// Add the data for the newly obtained photos to the table
addPhotosToTable()
});
}
关于我做错了什么的任何提示?
您可以在此处查看完整源代码:http://luisargote.com/flickr/javascript/argote_flickr.js
最佳答案
你看到的是因为 .ajaxStop()附加一个新的 事件处理程序,并且您每次都附加一个新的。只需将它移到外面(但仍在 document.ready 处理程序内),如下所示:
// This gets the user ID from a given Flickr user page URL and does some presentation stuff
function getUserID() {
$("#moreRow").hide(350);
var usr = document.getElementById('user').value
var Req_addr = 'http://api.flickr.com/services/rest/?method=flickr.urls.lookupUser&api_key=' + API_key + '&url=http%3A%2F%2Fflickr.com%2Fphotos%2F' + usr + json
$.getJSON(Req_addr, function(data) {
// Once the user is known, data about its photos is requested
getPhotos(data.user.id)
});
// This hides the user data panel
$("#userInfo").hide(0);
// This hides the settings panel
$("#settings").hide(0, function() {
$("#loader").slideDown(750);
});
}
// This is what displays the photos when all of the AJAX requests have received their responses (ajaxStop)
$("#photos").ajaxStop(function() {
// the page counter is incremented for the next page to be requested next time
page += 1
// Add the data for the newly obtained photos to the table
addPhotosToTable()
});
另一种方法是(如果由于某种原因 #photos 被吹走了),将它留在函数内部的位置并使用 .one()像这样:
$("#photos").one("ajaxStop", function() {
这将运行处理程序一次,然后解除绑定(bind),给出你想要的效果......但除非元素在某处被破坏(听起来不像是这样)否则坚持在外部绑定(bind)一次,没有理由做额外的工作。
关于javascript - .ajaxStop 回调函数被多次执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4254891/
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法
我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试
如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否