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();
}