简体   繁体   English

Javascript:从循环中断

[英]Javascript: Break from loop

I want to keep checking a list until it finds an item that is available. 我想继续检查列表,直到找到可用的项目。 For each item in the list it will make a post request to check if the item is available. 对于列表中的每个项目,它将发出一个帖子请求以检查该项目是否可用。 I want to keep it asynchronous. 我想让它保持异步。 Also note that i am using ajaxq function not ajax function, that is because i am using this queuing script http://code.google.com/p/jquery-ajaxq/ . 另请注意,我使用的是ajaxq函数而不是ajax函数,这是因为我正在使用这个排队脚本http://code.google.com/p/jquery-ajaxq/ So it won't be over before it's started sorta-thing. 所以在它开始排序之前它不会结束。 I'm sure thats not the problem though. 我确定这不是问题所在。

I need a way to break out of the loop once a item is available, so i can't just use a callback function because it won't be able to break out of the loop inside a function. 一旦项目可用,我需要一种方法来摆脱循环,所以我不能只使用回调函数,因为它无法突破函数内部的循环。 So i thought incrementing a variable if its done and using a do-while loop would work, but it just freezes my browser like it's a never-ending loop. 所以我认为增加一个变量,如果它完成并使用do-while循环可以工作,但它只是冻结我的浏览器,就像它是一个永无止境的循环。

Any suggestions on how do fix this or do it a better way would be great. 关于如何解决这个问题或者做得更好的方法的任何建议都会很棒。

do {

    var d = 0;
    for(var i in list) {

        var item = list[i];

        $.ajaxq('queue', {
            type: 'POST',
            url: baseURL + '/ChangeItem/Check',
            data: {
                'newItem': item, 
                'purchaseItem': false
            },
            error: function(jqXHR, textStatus) {
                alert(textStatus);
            },
            dataType: 'text',
            success: function(data) {
                if(thisObject.isNotTaken(data)) {
                    d++;
                    thisObject.claimItem();
                }
            }
        });
    }

} while(d == 0);

You can use a recursive function: 您可以使用递归函数:

function ChangeItem(list, index) {
        var item = list[index];

        $.ajaxq('queue', {
            type: 'POST',
            url: baseURL + '/ChangeItem/Check',
            data: { 'newItem': item, 'purchaseItem': false },
            error: function(jqXHR, textStatus) { alert(textStatus); },
            dataType: 'text',
            success: function(data) { 
                 if(thisObject.isNotTaken(data)) { thisObject.claimItem(); doWhateverYouWantNext(); } 
                 else ChangeItem(list, index+1);  
            }

        });
}

The fact that the requests will be queued only guarantees that they will be executed seuqentially, and that the first request will have finished before the second starts. 请求将排队的事实仅保证它们将按顺序执行,并且第一个请求将在第二个请求开始之前完成。 It does not mean that your code to enqueue the second request will wait until the first request is finished. 这并不意味着您排队第二个请求的代码将等到第一个请求完成。 So ajaxq does not help you either way here. 所以ajaxq在这里没有任何帮助。 You'd have to fall back to a recursive function, that invokes itself from the AJAX callback. 你必须回退到一个递归函数,它从AJAX回调中调用自己。

Having said that, you'll notice this'll cause a series of requests to your server, and presumably a series of database lookups. 话虽如此,您会注意到这会导致对您的服务器发出一系列请求,并且可能是一系列数据库查找。 You may find that it'd be a much neater approach to send teh entire list of items to the server, and return the first match from there. 您可能会发现将整个项目列表发送到服务器并从那里返回第一个匹配是一种更简洁的方法。

Please try this: 请试试这个:

var d = 0;
for(var i in list) {

    if(d == 0)
    {   
        var item = list[i];

        $.ajaxq('queue', {
            type: 'POST',
            url: baseURL + '/ChangeItem/Check',
            data: {
                'newItem': item, 
                'purchaseItem': false
            },
            error: function(jqXHR, textStatus) {
                alert(textStatus);
            },
            dataType: 'text',
            success: function(data) {
                if(thisObject.isNotTaken(data)) {
                    d++;
                    thisObject.claimItem();
                }
            }
        });

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM