问题描述
我在使用跨使用requirejs不同的模块在循环中调用函数的问题。内环路的函数调用驻留在模块A和执行模块B中触发了使用jQuery Ajax请求的功能。循环打完不同的要求使用不同的参数的每一次迭代传递到模块B的功能触发了Ajax请求。当Ajax请求的成功执行的功能,我发现我所有的参数值总是最后一个Ajax调用制成的价值观,所有4个独立的Ajax调用。
I'm having an issue with calling functions within a loop across different modules using requirejs. The function call within the loop resides in module A and executes a function in module B that fires off an Ajax request using jQuery. Each iteration of the loop fires off a different request with different arguments being passed to module B's function that fires off the Ajax request. When the success function of the Ajax request executes, I find that all my argument values are always the values of the last Ajax call made, for all 4 separate Ajax calls.
我已经做了一些谷歌上搜索这听起来像这是一个pretty的通病一个循环中执行的函数时。此修复程序往往是打出来的函数调用成不同的功能,创建一个不同的范围。由于我的循环和Ajax调用是在2个不同的模块,我认为这将解决这个问题,但它仍然存在。
I've done some googling and it sounds like this is a pretty common problem when executing a function within a loop. The fix tends to be to break out the function call into a different function, creating a different scope. Since my loop and Ajax calls are in 2 different modules I had assumed this would solve that issue, however it still persists.
我试着像其他堆栈溢出的职位一些解决方案:JSlint错误'不要在循环中进行的功能。导致质疑关于JavaScript本身和How传递参数的setTimeout调用?没有成功定义一个匿名函数。任何人有什么想法?
I've tried some solutions in other stack overflow posts like:JSlint error 'Don't make functions within a loop.' leads to question about Javascript itself and How to pass parameter to an anonymous function defined in the setTimeout call? without success. Anyone have any idea?
样品code for循环模块A:
Sample code for loop module A:
define(["mpos"],
function(mpos){
var monitor = {
startMonitoring : function(poolObj){
// Start Monitoring
$.each(mpos.msgs, function(action,callback){
poolObj.action = action;
mpos.sendApiRequest(poolObj,action,callback);
});
}
};
return monitor;
}
);
样品code阿贾克斯模块B - 这个模块被引用在模块A MPOS
Sample code for Ajax module B - this module is referenced as mpos in module A
define(["mule","constants"],
function(mule,constants){
var mpos = {
sendMessage : function(postData,callback,$poolOut){
return $.ajax({
'type':'post',
'url':constants.URLS.proxy,
'data':{'url':postData},
success : function(data){
// if we have $poolOut we know this is a mpos call
if($poolOut != undefined){
var keys = Object.keys(data);
// add poolOut to data
data.poolOut = $poolOut;
var poolObj = $poolOut.data('poolObj');
if(poolObj){
var action = poolObj.action;
console.log(poolObj,action);
if(action){
if(action == "getuserstatus"){
mule.registerPool(poolObj);
}
} else {
log.error("No action on poolObj while attempting to calculate the need for a registerPool call");
}
}
}
// parse data
callback.apply(this, data);
},
error : function(x,h,r){ ... },
dataType : 'json'
});
},
sendApiRequest : function(poolObj,action,callback){
var url = poolObj.url + '&page=api&action=' + action;
var $poolOut = constants.cache.monitorOutput.find('.pool-out.' + poolObj.id);
var dfd = mpos.sendMessage(url,callback,$poolOut);
$.when(dfd).always(function(){
var refreshTimer = setTimeout(function(){
if(constants.state.monitorEnabled){
mpos.sendApiRequest(poolObj, action, callback);
}
}, poolObj.refreshRate);
});
},
msgs : {
"getuserstatus" : function(data){ ... },
"getpoolstatus" : function(data){ ... },
"getuserworkers" : function(data){ ... },
"getuserbalance" : function(data){ ... }
}
};
return mpos;
}
);
谢谢!
推荐答案
注:我假设 $ poolOut.data('poolObj')
被用来寻找在呼叫通过 poolObj
实例 startMonitoring
,并将每次返回同一个实例。
NOTE: I am assuming that $poolOut.data('poolObj')
is being used to find the poolObj
instance passed in the call to startMonitoring
, and will return the same instance each time.
您的国家循环打完不同的要求使用不同的参数的每次迭代被传递到模块B的功能触发了Ajax请求。
You state, "Each iteration of the loop fires off a different request with different arguments being passed to module B's function that fires off the Ajax request."
这个说法是不正确的。每次迭代打完不同的请求的第一个参数 poolObj
作为的一样的每次迭代。
This statement is not correct. Each iteration fires off a different request with the first argument poolObj
being the same in each iteration.
在你的。每次
迭代,要覆盖的 poolObj.action
值每次调用之前 sendApiRequest
。
In your .each
iteration, you are overwriting the value of poolObj.action
before each call to sendApiRequest
.
在AJAX的成功处理,这很可能调用的在的所有迭代完成, poolObj.action
的价值将有你的价值其设置为在上一次迭代
In the AJAX success handler, which is likely invoked after all iterations have completed, the value of poolObj.action
will have the value you set it to in the last iteration.
要解决这个问题,我想你需要传递动作
作为参数传递给的sendMessage
,也使一个单独的值被存储在封闭件的每个函数调用
To solve this, I think you need to pass action
as a parameter to sendMessage
, too, so that a separate value is being stored in the closure for each function call.
var mpos = {
sendMessage : function(postData,action,callback,$poolOut){
return $.ajax({
'type':'post',
'url':constants.URLS.proxy,
'data':{'url':postData},
success : function(data){
// if we have $poolOut we know this is a mpos call
if($poolOut != undefined){
var keys = Object.keys(data);
// add poolOut to data
data.poolOut = $poolOut;
var poolObj = $poolOut.data('poolObj');
if(poolObj){
// action is not guaranteed to be the same as poolObj.action here,
// since poolObj.action may have changed since this function was first called
console.log(poolObj,action);
if(action){
if(action == "getuserstatus"){
mule.registerPool(poolObj);
}
} else {
log.error("No action on poolObj while attempting to calculate the need for a registerPool call");
}
}
}
// parse data
callback.apply(this, data);
},
error : function(x,h,r){ ... },
dataType : 'json'
});
},
sendApiRequest : function(poolObj,action,callback){
var url = poolObj.url + '&page=api&action=' + action;
var $poolOut = constants.cache.monitorOutput.find('.pool-out.' + poolObj.id);
var dfd = mpos.sendMessage(url,action,callback,$poolOut);
$.when(dfd).always(function(){
var refreshTimer = setTimeout(function(){
if(constants.state.monitorEnabled){
mpos.sendApiRequest(poolObj, action, callback);
}
}, poolObj.refreshRate);
});
},
msgs : {
"getuserstatus" : function(data){ ... },
"getpoolstatus" : function(data){ ... },
"getuserworkers" : function(data){ ... },
"getuserbalance" : function(data){ ... }
}
};
这篇关于使用requirejs一个循环内的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!