嗨,我正在制作一个javascript脚本,现在变得很难编辑,也很难为其他人所理解,我将其放在此处,希望有人能够理解它并提供一些建议或帮助

function fetchMember(id, select, sitename, total) {
    return function() {
        progress();
        $.ajax({
            type: 'POST',
            url: "script.php",
            data: $("#fetch").serialize() + "&id=" + id,
            success: function(data) {
                isUser = ($(data).text().indexOf("Invalid User") == -1);
                if (isUser) {
                    username = $(data).find(".normal").text();
                    saved = id - invalid;
                    $.ajax({
                        type: 'POST',
                        url: "save.php",
                        data: {'username': username},
                        success: function(data) {
                            $("#test").append(id+" "+data + "<br />");
                            select.text(sitename+"("+saved+"/"+total+")"); //Updating numbers of fetched profiles on the frontend
                        }
                    });
                }
                else
                invalid++; //loop again here because a user wan't valid
                progress();
            }
        });
    }
}
for (i = 0; i < members; i++) {
            fetched++;
            setTimeout(fetchMember(fetched, select, sitename, total), wait*i);
        }


基本上,我需要做的是再次循环,如果在操作结束时有一些无效的用户,真的很感谢您的帮助

最佳答案

我不知道这段代码是否可以帮助您,尽管它并未完全适合您的情况并且未经测试。主要原理是memberFetch函数的递归调用。在这种情况下,无需超时-在收到最后一个响应之前,它不会向服务器发出任何新请求。随时问任何问题,但请尝试自己尝试一下:)

var currentId = 0; // Current member id
var membersNum = 10; // There are 10 members from 0 to 9
var neededValidUsersNum = 5; // We need only 5 valid users...
var valudUsersNum = 0; // ... but now we have 0 of them

// Let's make an array of all possible id's
// It will be a queue - we will try to fetch the first id
// In case of success - save data, remove that id from the queue, fetch the nex one
// Otherwise - put it at the back of the queue to try it again later
var possibleIds = [];
for (var i = 0; i < membersNum; i++) {
    possibleIds.push(i);
}

// Fetched user data storage
var userData = {};

function fetchMember(id) {
    var data = "some data";

    $.post('script.php', data)
        .done(function(responseData){
            onFetchMemberDone(id, responseData);
        })
        .fail(function(){
            onFetchMemberFail(id);
        });
}

function onFetchMemberDone(id, responseData){
    // Save recieved user data
    userData[id] = responseData;
    // Bump valid users num
    valudUsersNum++;
    // If there are not enough valid users - lets continue:
    if (valudUsersNum < neededValidUsersNum) {
        // Remove valide user from the queue (it was the first one)
        possibleIds.shift();
        // try to fetch the next one
        var nextPossibleId = possibleIds[0];
        fetchMember(nextPossibleId);
    }
}

function onFetchMemberFail(id){
    // add failed user to the end of the queue
    possibleIds.push(id);
    // try to fetch the next one
    var nextPossibleId = possibleIds[0];
    fetchMember(nextPossibleId);
}

// Lets launch the cycle! It doesn't look like one because it works through recursive calls
onFetchMember(0);

关于javascript - 需要根据条件再次循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25569396/

10-09 20:53