好的,这是我的问题。我正在使用使用分页的API。我一直循环,直到响应回来是一个空白数组。但是,这不起作用,代码只是挂起。每个循环,我都会增加页面变量以获得下一页。
// Function to get a list of 50 assignments.
function getAssignments(token, instuctureSubdomain, course, page, callback) {
var settings = {
"async": true,
"crossDomain": true,
"url": "https://" + instuctureSubdomain + ".instructure.com/api/v1/users/self/courses/" + course + "/assignments?per_page=50&page=" + page,
"method": "GET",
"headers": {
"authorization": "Bearer " + token,
"cache-control": "no-cache"
}
}
$.ajax(settings).done(function (response) {
callback(response);
});
};
var loop = true;
var page = 1;
while (loop) {
getAssignments(token, instuctureSubdomain, item.id, page, function (response) {
if (response.length == 0) {
// End the loop because the response returned a blank array.
loop = false;
} else {
response.forEach(function (item) {
// Add the assignment to an array...
});
page++
};
});
};
答案 0 :(得分:0)
这是我写它的方式。它不会因大量请求而延迟/冻结您的网页,并且几乎同步地调用它。
<script>
function loop (i) {
$.get("http://example.com/api/" + i + ".php", function (data) {
array = process(data);
if (array.length > 0) {
loop(i + 1);
}
});
}
loop(1);
</script>