0

I am trying to create a boolean function that returns true if the file is there and startInterval to output to my textbox, else return false if it isn't there. I looked up an example here: example

Here is my boolean function:

function isGatewayProcessing(callback){
$.ajax({
    url: fileToCheck,
    type:'HEAD',
    cache: 'no-cache',
    error: function()
    {
        //file not exists
        console.log("File is not there!");
        return callback(false);
    },
    success: function()
    {
        //file exists
        console.log("File is there! Continue to output status..." + fileToCheck);
        return callback(true);
    }
});
}

function myCallback(exist) {
    if(exist) {
     console.log("The file is there, starting interval...");
     startInterval();
} else {
     console.log("The file is NOT there, reseting interval...");
     resetInterval();
}
}

isGatewayProcessing(myCallback);

Everything works fine except sometimes when I reload the page, even when the file is not there (console.log shows fileToCheck = undefined on page reload) it will resolve to true and start the interval, which outputs my whole html page to the statusbox. Originally, I thought it was because I had the cache option set to true, so I changed it to false, but still no luck.

Does anyone know why the function may be resolving to true if the file is not there?

Alternately, I can just add a workaround in this if statement and it will work, but I still want to know why it is resolving to true when the file is not there.

if(exist && fileToCheck) { //add fileToCheck here 
     console.log("The file is there, starting interval...");
     startInterval();
}
tiger_groove
  • 956
  • 2
  • 17
  • 46
  • 2
    cache is boolean so should be `cache: false,` http://api.jquery.com/jquery.ajax/ – Andrew Lohr Mar 14 '18 at 18:00
  • What exactly does the server side code do? If the file is not there does it really return an HTTP error? – Pointy Mar 14 '18 at 18:01
  • 3
    If `fileToCheck` is undefined it would be as if you didn't set a url and the default url is the current page, so you're checking if the current page exists. – Musa Mar 14 '18 at 18:02
  • @AndrewLohr Good catch! let me fix that. – tiger_groove Mar 14 '18 at 18:05
  • @Pointy Yes, we get a 404 back if the file is not there. The server side code check the user input field and looks for the file location based on the input, something like this `fileToCheck=/path/to/my/file + userInput` this happens only on first instance of page refersh with no userInput so file is undefined. – tiger_groove Mar 14 '18 at 18:09
  • @Musa So how can I account for function boolean to resolve to false if fileToCheck is undefined? I edited my post with a workaround, but is there a way to check in the ajax call? – tiger_groove Mar 14 '18 at 18:11
  • if fileToCheck is constant you can keep it outside document.ready and wrap everything else inside document.ready, if you are getting it dynamically then make you isGatewayProcessing call after getting dynamic value – Rahul R. Mar 14 '18 at 18:24

0 Answers0