【发布时间】:2013-01-16 09:21:49
【问题描述】:
我尝试为我正在处理的项目的异步调用中的排序问题创建一个简单的解决方案。
我找到的最佳解决方案是: 我附加了一个事件来检查前置要求是否已完成,如果是,我删除事件侦听器并执行该功能。
每个函数一旦完成就会调用事件,这样每个人都将等待直到可以运行。
这是代码:
$(function() {
$(document).on('bmadone',function() {
if(beepmeapp.done_arr['fb-init']){
$(document).off('bmadone','#bma-root',this);
getBMAUserRoutes();
}
});
});
函数(用于测试)正在这样做:
function getBMAUserRoutes(){
$.ajax({
url : '/bma/users/fb',
type : 'GET',
data : {
access_token: 'abc'
},
success : function( data ) {
console.log('getBMAUser: success');
beepmeapp.done_arr['user-init'] = true;
$('#bma-root').trigger('bmadone');
},
error : function( xhr, err ) {
console.log('getBMAUser: error');
}
});
}
效果很好,但我遇到的问题是它进入了一个永无止境的循环。 不知道为什么,但它看起来像:
$(document).off('bmadone','#bma-root',this);
不移除监听器...
我是 JS / Jquery 和所有这些客户端开发的真正新手,所以我想我可能缺少一些基本的东西。
【问题讨论】:
-
"this" 可能不是你想的那样。 ;)
-
而不是
$(document).off('bmadone','#bma-root',this);尝试$(document).off('bmadone');假设这会起作用 -
问题在于 $(document).off('bmadone');将删除所有处理程序,我只需要删除一个
标签: javascript jquery javascript-events event-handling