使用requirejs在循环内的函数

我在使用requirejs在不同模块的循环中调用函数时遇到问题。 循环中的函数调用驻留在模块A中,并在模块B中执行一个函数,该函数使用jQuery触发Ajax请求。 循环的每次迭代都会触发一个不同的请求,并将不同的参数传递给模块B的函数,该函数将触发Ajax请求。 当Ajax请求的成功函数执行时,我发现对于所有4个单独的Ajax调用,我的所有参数值始终是最后一次调用Ajax的值。

我做了一些谷歌搜索,听起来这是在循环中执行函数时非常常见的问题。 修复往往是将函数调用分解为不同的函数,创建不同的范围。 由于我的循环和Ajax调用在2个不同的模块中,我认为这将解决该问题,但它仍然存在。

我在其他堆栈溢出post中尝试了一些解决方案,例如: JSlint错误’不要在循环中创建函数’。 导致有关Javascript本身以及如何将参数传递给setTimeout调用中定义的匿名函数的问题? 没有成功。 有人有什么想法吗?

循环模块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; } ); 

Ajax模块B的示例代码 – 该模块在模块A中引用为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; } ); 

谢谢!

注意:我假设$poolOut.data('poolObj')用于查找在调用startMonitoring传递的poolObj实例,并且每次都返回相同的实例。

您声明,“循环的每次迭代都会触发一个不同的请求,并将不同的参数传递给模块B的函数,该函数将触发Ajax请求。”

这种说法不正确。 每次迭代都会触发一个不同的请求,第一个参数poolObj在每次迭代中都是相同的

.each迭代中,在每次调用poolObj.action之前,您将覆盖poolObj.action的值。

在AJAX成功处理程序(可能所有迭代完成调用)中, poolObj.action的值将具有您在上一次迭代中将其设置为的值。

为了解决这个问题,我认为你需要将action作为参数传递给sendMessage ,以便在闭包中为每个函数调用存储一个单独的值。

 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){ ... } } };