我想在循环中检查列表中的3000个项目。在每一步我必须在页面上进行异步httprequest。使之成为一个递归函数IST扔InternalError: too much recursion
这是到目前为止我的代码:JavaScript异步调用循环
var iteratorObject = new function(){
this.currentStep = -1;
this.iterationList = {};
this.start = function(list){
this.iterationList = list;
this.next(0);
}
this.next = function(value) {
this.currentStep = value;
if(this.currentStep +1 == this.iterationList.length){
return
}
var currentID = this.iterationList[this.currentStep];
//Do Aysnc HttpRequest on succeed call this.next(value +1);
}
}
var iterator = iteratorObject;
var iterationList = Object.keys([Object]);
iterator.start(iterationList);
如何更改我的代码以避免递归函数,只有一次处理一个请求?
你不能在'for'循环,除非你想要写一个框架,做_promises_做到这一点。你看起来像你很接近 - 只需重写'next',以便查看'currentStep'而不需要'value'。 – 2015-02-09 00:02:00
在回调中调用下一个仍然是递归函数?我有一种方法可以在currentStep上调用next()函数来获取事件列表。 对不起,我有点雏在JS – 2015-02-09 00:06:23
你可以看看这个jsbin我准备:http://jsbin.com/kacabu/1/edit?js,console – 2015-02-09 00:47:17