0

Alright so what I want is to start looping an ajax call once I press a button, the ajax will communitcate with the database and depending on the response, I want it to either loop, and re-do the exact same call, or stop the call and go to another function.

I have the following code right now:

     function checkResult(session, ip, key){
    $.ajax({
        url: "myUrl"+key,
    }).success(function(returnData){
        returnData = returnData.replace("\n", "");
        returnData = returnData.replace(" ", "");
        if(returnData == 1){//Everything is fine, stop the call
            clearInterval(interval);
            console.log("completed");
        }else if(returnData == 2){//Something went wrong, stop the call
            clearInterval(interval);
            console.log("Something went wrong");
        }else if(returnData == 0){//No result found, repeat call
            console.log("Nothing was found");
            return returnData;
        }
    });
}

function repeatCall(session, ip, key){
    console.log("Repeating the request");
    interval = setInterval(function(){ var status = checkPayment(session, ip, key); if(status != 0){
        clearInterval(interval);
        checkResult(status)}}, 1000);
}

It's currently looping through the whole call only once, it logs "Nothing was found" in my console. What's the correct way of getting this to work???

killstreet
  • 1,251
  • 2
  • 15
  • 37
  • In addition, do NOT use interval. Instead use `if (returnData == 0){setTimout(function() { checkResult(session, ip, key)},1000)}` – mplungjan Oct 23 '15 at 11:48
  • @mplungjan But the timeout will only repeat it once, I want it to keep repeating untill it has found some result – killstreet Oct 23 '15 at 11:52
  • No. It will KEEP repeating for as long as returnData is 0. Think it through :) You do need to keep track of the parameters session, ip and key somewhere – mplungjan Oct 23 '15 at 11:53

1 Answers1

0

Errr, if I understand you correctly, you should be able to do something like this...

function makeAJAXRequest(session, ip, key)
{
    $.ajax({
        type:'POST', // or GET
        url: "myUrl"+key,
        success: function(status){
            returnData = returnData.replace("\n", "");
            returnData = returnData.replace(" ", "");

            if(returnData == 1)
            {   //Everything is fine, stop the call
                clearInterval(interval);
                console.log("completed");
            }
            else if(returnData == 2)
            {   //Something went wrong, stop the call
                clearInterval(interval);
                console.log("Something went wrong");
            }
            else if(returnData == 0)
            {   //No result found, repeat call
                console.log("Nothing was found");
                makeAJAXRequest(session, ip, key);
            }
        },
        error: function(status, err){
            // dome something
        }
    });
}
An0nC0d3r
  • 1,275
  • 13
  • 33
  • you do not need interval nor to clear it if you call the function in the success. Also you cannot return from an ajax call – mplungjan Oct 23 '15 at 11:45
  • Yeh I've purely focused on him wishing to call the same method when no data is found. – An0nC0d3r Oct 23 '15 at 11:46