Reputation: 4832
I am trying to create a loop that each time, retrieves another pages' content via $.post
and upon completion, does the next page, until an element on that page shows 0 results.
function scrapeIt() {
$(".currentPage").html("Current Page: " + page);
$.post("scrapePosts.aspx", { page: page, search: keyword }, function (data) {
$(".status").html(data);
if ($("#count").html() == "10") {
scrapeIt();
} else {
alert("Stopping...");
}
});
page++;
}
$(document).ready(function () {
var page = 1;
var keyword;
var stillGettingResults = true;
$("#go").click(function () { //Start Button
keyword = $("#keyword").val(); // Textbox
$(".status").html("Loading...");
scrapeIt();
});
});
The idea was for the scrapeIt()
function to call itself again, but only when it has completed the post request. It seems to just freeze though.
Upvotes: 0
Views: 409
Reputation: 93694
var page = 1
and var keyword
should both be declared in global scope, otherwise they are undefined
in function scrapeIt()
.
Your page could appear to be freezing because no page
or keyword
is being sent as part of the post request, and the server may not understand the request.
Upvotes: 3
Reputation: 8209
Because of recursion page is always zero. You need to increment the page variable in the post callback not at the end of scrapeIt.
$.post("scrapePosts.aspx", { page: page, search: keyword }, function (data) {
page++;
$(".status").html(data);
if ($("#count").html() == "10") {
scrapeIt();
} else {
alert("Stopping...");
}
Upvotes: 0
Reputation: 1954
see setTimeout() $.post
is not asynchronous i guess and constantly calling it will freeze the browser thread
Upvotes: 0