Javascript在一个回调函数中打破了一个for循环 [英] Javascript breaking a for loop inside a callback function

查看:176
本文介绍了Javascript在一个回调函数中打破了一个for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下的代码:

$ $ $ $ $ $ $ $ $ $ $ $ $如果($ // some conditon){
obj.onload();
} else {
obj.onerror();



$ b for(var i = 0; i <4; i ++){

test({
onload:function(e){
//一些代码运行
},
onerror:function(e){
break;
}


});



$ b $ p
$ b

要点是test()函数是一个XHR请求(它实际上是Appcelerator Titanium平台的API,所以我无法控制它),我正在循环一些东西来调用测试函数。我需要打破onerror函数的循环,但我得到一个错误,说中断不在循环或switch语句。我如何重写这个?

解决方案

如果您的代码示例确实代表了一些实际的代码(即所有的处理都在相同的事件循环滴答),您可以执行以下操作:

pre code函数test(obj){

if (some_condition){
return obj.onload();
} else {
return obj.onerror();
}
}

var result;
for(var i = 0; i <4; i ++){

result = test({
onload:function(e){
// some code运行
返回true;
},
onerror:function(e){
return false;
}


});

if(!result){
break;






$ b如果有异步完成,按顺序调用 test ,而不是并行调用。例如,

  function test(obj){
doSomeAjaxRequest(arguments,function(err,result){
if(some_condition){
obj.onload();
} else {
obj.onerror();
}
});


var f = function(i){
if(i> = 4)return;
测试({
onload:function(e){
//一些代码运行
f(i + 1);
},
onerror:函数(e){
break;
}
});
}

f(0);


I have code like the following:

function test(obj) {

    if(//some conditon) {
        obj.onload();
    }else{
        obj.onerror();
    }
}


for(var i=0;i<4;i++){   

    test({
        onload:function(e){          
            //some code to run
        },
        onerror:function(e){
            break;
        }


    });
}

The gist is the test() function is a function to make an XHR request (it is actually an API of the Appcelerator Titanium platform so I have no control over it) and I'm looping something to call the test function. I need to break the loop on the onerror function, but I get an error saying the break is not inside a loop or switch statement. How can I rewrite this?

解决方案

If your code sample does represent some actual code (i.e. all the processing is done in the same event loop tick), you may do the following:

function test(obj) {

    if (some_condition) {
        return obj.onload();
    } else {
        return obj.onerror();
    }
}

var result;
for(var i=0; i<4; i++){   

    result = test({
        onload:function(e){          
            //some code to run
            return true;
        },
        onerror:function(e){
            return false;
        }


    });

    if (!result) {
        break;
    }
}

Otherwise, if there is something asynchronous done, you have to call test sequentially, and not in parallel. For example,

function test(obj) {
    doSomeAjaxRequest(arguments, function (err, result) {
        if (some_condition) {
            obj.onload();
        } else {
            obj.onerror();
        }
    });
}

var f = function (i) {
    if (i >= 4) return;
    test({
        onload:function(e){          
            //some code to run
            f(i+1);
        },
        onerror:function(e){
            break;
        }
    });
}

f(0);

这篇关于Javascript在一个回调函数中打破了一个for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆