我在使用requirejs在不同模块循环中调用函数时遇到问题。循环中的函数调用驻留在模块A中,并在模块B中执行一个使用jQuery触发Ajax请求的函数。循环的每次迭代都会引发一个不同的请求,并将不同的参数传递给模块B的函数,该函数将触发Ajax请求。执行Ajax请求的成功函数时,我发现所有的参数值始终是最后一次Ajax调用的值,适用于所有4个独立的Ajax调用。使用requirejs循环中的函数
我做了一些Google搜索,听起来像这是在循环内执行函数时很常见的问题。修复倾向于将函数调用分解为不同的函数,从而创建不同的范围。由于我的循环和Ajax调用位于两个不同的模块中,我假设这可以解决该问题,但它仍然存在。
我已经尝试了其他堆栈溢出帖子中的一些解决方案,如: JSlint error 'Don't make functions within a loop.' leads to question about Javascript itself和How to pass parameter to an anonymous function defined in the setTimeout call?没有成功。任何人有任何想法?
的回路模块示例代码:适合于Ajax模块B
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;
}
);
示例代码 - 这个模块被引用在模块MPOS一个
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;
}
);
谢谢!
您是否试图在相同模块下复制您的非工作代码?我想问题是你的代码,而不是与requirejs,但也许我错了... – inf3rno