this is my summarized code :
var searchCount = 0;
function requestResults() {
$.get("http://www.example.com/example.ashx",
{ i: "test" },
function (data) {
if (data) {
$(data).find("xmlTestNode").each(function (index, item) {
// do someting (1) ...
});
} else {
// dont try to search more than 20 seconds
searchCount += 1;
if (searchCount <= 4) {
setTimeout(function () {
requestResults();
}, 5000);
} else {
searchCount = 0;
// there is no result for 20 seconds searching
};
};
}, "text");
};
this code gets some data from .ashx handler. as the handler needs to search and answering may take some times, this function sends it @.get request each 5 seconds. if handler have found results, it will be OK and the line "do something (1)" will be run.
if handler didn't answer for 20 seconds, the function will stop repeating its request.
sometimes user may want to stop searching within 20 seconds, so he hit Stop button.
I can stop showing data if Stop button is clicked, but is there any way to stop $.get request from getting data? something like BREAK in functions.
the problem is when user try to search different queries quickly (multiple search in less than 5 seconds), many $.get requested were sent to server. what is the best solution to handle this problem?